- android:showText=“true” 是否显示on/off文字,true显示 false不显示
- android:splitTrack=“true” 是否分割轨迹并为开关可抽出留出间隙
- android:switchMinWidth=“60dp” Switch的最小宽度
- android:switchPadding=“10dp” 开关和文字之间的最小间距
- android:switchTextAppearance=“@style/TextAppearance.AppCompat” Switch上显示的文字外观样式
- android:textOff=“男” 开关处于未选中/“关闭”状态时使用的文字
- android:textOn=“女” 开关处于选中/“打开”状态时使用的文字
- android:textStyle=“bold” 文字的样式(普通、粗体、斜体、粗体|斜体)
- android:thumb=“@mipmap/xx” 设置Switch滑动的图片
- android:thumbTextPadding=“5dp” 开关Switch内文本两侧的填充量
- android:thumbTint=“@color/black” 着色以应用于滑动的按钮
- android:thumbTintMode=“xx” 用于应用滑动按钮色调的混合模式
- android:track=“@mipmap/xx” 设置 Switch的背景
- android:trackTint=“xx” 着色以应用于轨迹
- android:trackTintMode=“xx” 用于应用轨迹色调的混合模式
- android:typeface=“monospace” 设置文本的字体(普通、无、衬线、单空格)
public class MainActivity extends AppCompatActivity {
private Switch switch_gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initClick();
}
private void initView() {
switch_gender = findViewById(R.id.switch_gender);
}
private void initData() {
switch_gender.setChecked(true);
}
private void initClick() {
switch_gender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "women", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "men", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<Switch
android:id="@+id/switch_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="true"
android:text="性别"
android:textOff="男"
android:textOn="女"/>