UI组件之TextView及其子类(三)ToggleButton和Switch
生活随笔
收集整理的這篇文章主要介紹了
UI组件之TextView及其子类(三)ToggleButton和Switch
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ToggleButton、Switch、CheckBox和RadioButton都是繼承自android.widget.CompoundButton,意思是可選擇的,因此它們的用法都很類似。CompoundButton有兩個(gè)狀態(tài),分別是checked和not checked。
ToggleButton的屬性:
Switch組件的屬性:
android:thumb是選中時(shí)的背景
例:開關(guān)按鈕控制布局方向
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ToggleButtonandroid:id="@+id/toggleButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ToggleButton"android:textOff="橫向排列"android:textOn="縱向排列" /> <!-- android:thumb="@drawable/check" 使用自定義的drawable對(duì)象繪制開關(guān)按鈕 --><Switchandroid:id="@+id/switch1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Switch"android:textOff="橫向排列"android:textOn="縱向排列"android:thumb="@drawable/check" /> <LinearLayout android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button2" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button3" /> </LinearLayout> </LinearLayout>
MainActivity.java
ToggleButton toggle;Switch switcher;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);toggle=(ToggleButton) findViewById(R.id.toggleButton1);switcher=(Switch) findViewById(R.id.switch1);final LinearLayout test=(LinearLayout) findViewById(R.id.root);//ToggleButton和Switch的監(jiān)聽接口和復(fù)選框CheckButton的一樣CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton button, boolean checkedId) {// TODO Auto-generated method stubif(checkedId){//1表示垂直布局,0表示水平布局test.setOrientation(1);}else{test.setOrientation(0);}} };toggle.setOnCheckedChangeListener(listener);switcher.setOnCheckedChangeListener(listener);}
以后要學(xué)會(huì)自己定制跟家美觀的組件
推薦幾個(gè)好的博客:
http://blog.csdn.net/billpig/article/details/6634481
http://blog.csdn.net/luoweifu/article/details/11752035
總結(jié)
以上是生活随笔為你收集整理的UI组件之TextView及其子类(三)ToggleButton和Switch的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UI组件之TextView及其子类(二)
- 下一篇: UI组件之TextView及其子类(四)