Android開(kāi)發(fā)之開(kāi)關(guān)按鈕用法示例
本文實(shí)例講述了Android開(kāi)發(fā)之開(kāi)關(guān)按鈕用法。分享給大家供大家參考,具體如下:
效果如下:
以下是布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--定義一個(gè)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/thumb" android:checked="true"/> <!--定義一個(gè)可以動(dòng)態(tài)改變方向的線性布局--> <LinearLayout android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
活動(dòng)代碼實(shí)現(xiàn):
public class Home extends AppCompatActivity { ToggleButton toggle ; Switch switcher ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//顯示manLayout toggle = (ToggleButton) findViewById(R.id.toggle); switcher = (Switch) findViewById(R.id.switcher); final LinearLayout text = (LinearLayout) findViewById(R.id.text); CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) { if (isChecked) { //設(shè)置LinearLayout垂直布局 text.setOrientation(LinearLayout.VERTICAL); toggle.setChecked(true); switcher.setChecked(true); }else { //設(shè)置水平布局 text.setOrientation(LinearLayout.HORIZONTAL); toggle.setChecked(false); switcher.setChecked(false); } } }; toggle.setOnCheckedChangeListener(onCheckedChangeListener); switcher.setOnCheckedChangeListener(onCheckedChangeListener); } }
其中switch組建的 thumb:@drawable/thumb
項(xiàng)參考自://www.dbjr.com.cn/article/159100.htm
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android ListView獲得選項(xiàng)中的值
本篇文章主要介紹Android ListView,在Android開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)用到ListView 組件并有監(jiān)聽(tīng)事件,這里給大家一個(gè)簡(jiǎn)單實(shí)例,來(lái)說(shuō)明如何得到ListView選項(xiàng)中的值2016-07-07Android實(shí)現(xiàn)音量調(diào)節(jié)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁(yè)面布局及多媒體播放的設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09android自定義彈出框樣式的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了android自定義彈出框樣式的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05解決Android SELinux權(quán)限問(wèn)題記錄分析
這篇文章主要為大家介紹了解決Android SELinux權(quán)限問(wèn)題記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11詳解 Kotlin Reference Basic Types, String, Array and Imports
這篇文章主要介紹了詳解 Kotlin Reference Basic Types, String, Array and Imports的相關(guān)資料,需要的朋友可以參考下2017-06-06Android中協(xié)調(diào)滾動(dòng)布局的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中協(xié)調(diào)滾動(dòng)常用的布局實(shí)現(xiàn),類似這樣的協(xié)調(diào)滾動(dòng)布局,當(dāng)?shù)撞苛斜砘瑒?dòng)的時(shí)候,頂部的布局做響應(yīng)的動(dòng)作,我們都可以通過(guò)?AppBarLayout?和?MotionLayout?來(lái)實(shí)現(xiàn),本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06Android 廣播監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼
這篇文章主要介紹了Android 廣播監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02