上一篇:http://www.bkjia.com/kf/201112/115681.html
ToggleButton也是button的一種,不同體現在togglebutton分為按下和沒有按下兩種不同的狀態。這個程式通過使用togglebutton動態改變了linearlayout的布局方式。
public class ToggleButtonTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
if(arg1)
{
//設定LinearLayout垂直布局
test.setOrientation(1);
}
else
{
//設定LinearLayout水平布局
test.setOrientation(0);
}
}
});
}
}
public class ToggleButtonTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
if(arg1)
{
//設定LinearLayout垂直布局
test.setOrientation(1);
}
else
{
//設定LinearLayout水平布局
test.setOrientation(0);
}
}
});
}
}布局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定義一個ToggleButton按鈕 -->
<ToggleButton android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:checked="true"
/>
<!-- 定義一個可以動態改變方向的線性布局 -->
<LinearLayout android:id="@+id/test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定義一個ToggleButton按鈕 -->
<ToggleButton android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:checked="true"
/>
<!-- 定義一個可以動態改變方向的線性布局 -->
<LinearLayout android:id="@+id/test"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_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>
摘自 hn307165411的專欄