安卓学习-界面-ui-ToggleButton Switch

ToggleButton属性

xml属性 代码 说明
 android:checked  isChecked 是否选中
 android:textOn  setTextOn 开启时显示的文本
android:textOff setTextOff 关闭时显示的文本

Switch属性 

 

xml属性 代码 说明
 android:checked  isChecked 是否选中
 android:textOn  setTextOn 开启时显示的文本
android:textOff setTextOff 关闭时显示的文本
android:switchMinWidth   设置最小宽度
android:switchPadding setSwitchPadding

文字与选择按钮之间的距离

android:switchTextAppearance  

选择文字的样式

android:textStyle   文字样式
android:thumb setThumbResource

开关上文字的图片

android:track setTrackResource

开关轨道

android:typeface   文字样式

  

例子

 

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout1"
    android:padding="10dp" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置"
        android:textOff="垂直"
        android:textOn="水平" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        android:textOff="垂直"
        android:textOn="水平" />

</LinearLayout>

java

public class MainActivity extends Activity implements OnCheckedChangeListener {

    Switch switch1;
    ToggleButton toggleButton1;
    LinearLayout linearLayout1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        switch1=(Switch)findViewById(R.id.switch1);
        toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
        linearLayout1=(LinearLayout)findViewById(R.id.linearLayout1);

        switch1.setOnCheckedChangeListener(this);     
        toggleButton1.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
            }else{
                linearLayout1.setOrientation(LinearLayout.VERTICAL);
            }
        
    }
}
原文地址:https://www.cnblogs.com/weijj/p/3946235.html