android编程实例:动态控制布局遇到问题

Taony鲍永道 2016-03-23 12:40:46
在xml文件布局了程序没有出现问题,程序如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义一个ToggleButton按钮 -->
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向排列"
android:textOn="纵向排列"
android:checked="true"/>
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="横向排列"
android:textOn="纵向排列"
android:thumb="@drawable/check"
android:checked="true"/>
<!-- 定义一个可以动态改变方向的线性布局 -->
<LinearLayout
android:id="@+id/test"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义三个按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮一"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮二"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮三"/>
</LinearLayout>
</LinearLayout>
在java文件夹里就出现了问题,但我自己解决不了,求大师指导指导。代码如下:
package org.crazyit.togglebuttontest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
ToggleButton toggle;
Switch switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle = (ToggleButton) findViewById(R.id.toggle);
switcher = (Switch) findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
OnCheckedChangeListener listener = new OnCheckedChangeListener() //这句显示错误
{
@Override
public void onCheckChanged(CompoundButton button,boolean isChecked)
{
if (isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);//"1"显示错误
toggle.setChecked(true);
switcher.setChecked(true);
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);//"0"也显示错误
toggle.setChecked(false);
switcher.setChecked(false);
}
}
};
toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
希望各位大师帮忙解决下这个问题,我是初学android编程开发,遇到一些问题真的很烦恼。
...全文
244 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Taony鲍永道 2016-03-23
  • 打赏
  • 举报
回复
谢谢各位,非常感谢。 还有就是: 看一下LinearLayout的源代码就可以知道,setOrientation()这个函数的参数被IntDef注解包成了一个枚举,因此setOrientation函数的参数只能够传它枚举中定义的常量,你可以试试在参数里传 LinearLayout.HORIZONTAL 或者 LinearLayout.VERTICAL 试试,应该就不会报错了。 可以说的更具体写吗?我是android开发的新手,学习了一点点基础,希望能更详细些。 学习各位!
Taony鲍永道 2016-03-23
  • 打赏
  • 举报
回复
<LinearLayout android:id="@+id/test" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> final LinearLayout test = (LinearLayout)findViewById(R.id.test); 这里已经声明过了。 另外 onCheckChanged(CompoundButton button,boolean isChecked)写错了,应该是onCheckedChanged(CompoundButton buttonView, boolean isChecked)这个也试过了,还是不行。
wei220000 2016-03-23
  • 打赏
  • 举报
回复
onCheckChanged(CompoundButton button,boolean isChecked)写错了,应该是onCheckedChanged(CompoundButton buttonView, boolean isChecked)
bdmh 2016-03-23
  • 打赏
  • 举报
回复
test是什么鸟玩意,你从头到尾都没有声明这个对象
angelseedex 2016-03-23
  • 打赏
  • 举报
回复
看一下LinearLayout的源代码就可以知道,setOrientation()这个函数的参数被IntDef注解包成了一个枚举,因此setOrientation函数的参数只能够传它枚举中定义的常量,你可以试试在参数里传 LinearLayout.HORIZONTAL 或者 LinearLayout.VERTICAL 试试,应该就不会报错了。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧