一手遮天 Android - view(选择类): Switch 样式
一手遮天 Android - view(选择类): Switch 样式
示例如下:
/view/selection/SwitchDemo2.java
/**
* Switch - 状态切换控件
* setThumbResource() - thumb 的各种状态下的样式
* setTrackResource() - track 的各种状态下的样式
* setSwitchTextAppearance() - 文字(在 textOff 和 textOn 设置的文字)的样式
*/
package com.webabcd.androiddemo.view.selection;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Switch;
import com.webabcd.androiddemo.R;
public class SwitchDemo2 extends AppCompatActivity {
private Switch _switch2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_selection_switchdemo2);
_switch2 = (Switch)findViewById(R.id.switch2);
sample();
}
private void sample() {
_switch2.setThumbResource(R.drawable.selector_switch_thumb);
_switch2.setTrackResource(R.drawable.selector_switch_track);
// 参见 values/styles.xml 中的“MySwitchTextAppearance”
_switch2.setSwitchTextAppearance(this, R.style.MySwitchTextAppearance);
}
}
/layout/activity_view_selection_switchdemo2.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">
<!--
Switch - 状态切换控件
switchMinWidth - 控件的宽度
showText - 是否需要显示 textOff 和 textOn 指定的文本
textOn - 选中状态时,thumb 上显示的文本
textOff - 未选中状态时,thumb 上显示的文本
thumb - thumb 的各种状态下的样式(参见 drawable/shape_switch_thumb_pressed)
track - track 的各种状态下的样式(参见 drawable/shape_switch_thumb_pressed)
switchTextAppearance - 文字(在 textOff 和 textOn 设置的文字)的样式(参见 values/styles.xml 中的“MySwitchTextAppearance”)
-->
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/selector_switch_thumb"
android:track="@drawable/selector_switch_track" />
<!--
在 java 中设置 Switch 的 thumb, track, switchTextAppearance
-->
<Switch
android:id="@+id/switch2"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="true"
android:textOff="关"
android:textOn="开"
android:switchMinWidth="200dp" />
</LinearLayout>
/drawable/selector_switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
state_pressed="true" - 按下
state_pressed="false" - 抬起
-->
<item
android:state_pressed="true"
android:drawable="@drawable/shape_switch_thumb_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/shape_switch_thumb_unpressed" />
</selector>
/drawable/selector_switch_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
state_checked="true" - 选中
state_checked="false" - 未选中
-->
<item
android:state_checked="true"
android:drawable="@drawable/shape_switch_track_checked" />
<item
android:state_checked="false"
android:drawable="@drawable/shape_switch_track_unchecked" />
</selector>