狀態開關按鈕(ToggleButton)和開關(Switch),togglebuttonswitch

來源:互聯網
上載者:User

狀態開關按鈕(ToggleButton)和開關(Switch),togglebuttonswitch

ToggleButton支援的XML屬性及相關方法
1.android:checked----->setChecked(boolean)
----->設定該按鈕是否被選中
2.android:textOff----->設定當該按鈕的狀態關閉時顯示的文本
3.android:textON----->設定當該按鈕的狀態開啟時顯示的文本

 


Switch支援的XML屬性及相關方法
1.android:checked----->setChecked(boolean)
----->設定該按鈕是否被選中
2.android:textOff----->設定當該按鈕的狀態關閉時顯示的文本
3.android:textON----->設定當該按鈕的狀態開啟時顯示的文本
4.android:switchMinWidth----->setSwitchMinWidth(int)
----->設定該開關的最小寬度
5.android:switchPadding----->setSwitchMinWidth(int)
----->設定該開關與標題文本之間的空白
6.android:switchTextAppearance----->setSwitchTextAppearance(Context,int)
----->設定開關表徵圖上的文本樣式
7.android:textStyle----->setSwitchTypeface(TYpeface)
----->設定開關的文本風格
8.android:thumb----->setThumbResource(int)
----->指定使用自訂Drawable繪製該開關的開關按鈕
9.android:track----->setTrackResource(int)
----->指定使用自訂Drawable繪製該開關的軌道
10.android:typeface----->setSwitchTypeface(TYpeface)
-----設定該開關的文本的字型風格

 

 

     

 

Demo2\togglebutton_demo\src\main\res\layout\activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     tools:context=".MainActivity"> 7  8     <!--定義一個ToggleButton按鈕--> 9     <ToggleButton10         android:id="@+id/toggle"11         android:layout_width="wrap_content"12         android:layout_height="wrap_content"13         android:checked="true"14         android:textOff="橫向排列"15         android:textOn="縱向排列" />16     <!--定義一個可以動態改變方向的線性布局-->17     <LinearLayout18         android:id="@+id/test"19         android:layout_width="match_parent"20         android:layout_height="match_parent"21         android:orientation="vertical">22 23         <Button24             android:layout_width="wrap_content"25             android:layout_height="wrap_content"26             android:text="第一個按鈕"/>27         <Button28             android:layout_width="wrap_content"29             android:layout_height="wrap_content"30             android:text="第二個按鈕"/>31         <Button32             android:layout_width="wrap_content"33             android:layout_height="wrap_content"34             android:text="第三個按鈕"/>35     </LinearLayout>36 37 </LinearLayout>

 

Demo2\togglebutton_demo\src\main\java\com\ly\togglebutton_demo\MainActivity.java

 1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.widget.CompoundButton; 4 import android.widget.LinearLayout; 5 import android.widget.ToggleButton; 6  7 public class MainActivity extends Activity { 8     private ToggleButton toggle ; 9     private LinearLayout linear ;10     @Override11     protected void onCreate(Bundle savedInstanceState) {12         super.onCreate(savedInstanceState);13         setContentView(R.layout.activity_main);14         toggle = (ToggleButton) findViewById(R.id.toggle);15         linear = (LinearLayout) findViewById(R.id.test);16         toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {17             @Override18             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {19                 if (isChecked){20                     //如果選中了,就設定垂直布局21                     linear.setOrientation(LinearLayout.VERTICAL);22                 }else {23                     //否則就水平24                     linear.setOrientation(LinearLayout.HORIZONTAL);25                 }26             }27         });28     }29 30 31 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.