•屬性
textOn:控件打開時顯示的文字
textOff:控件關閉時顯示的文字
- showText:設置是否顯示開關上的文字(API 21及以上)
•用法
<?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:padding="10dp" android:gravity="center" android:orientation="vertical"> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textOn="on" android:textOff="off" android:showText="true"/> </LinearLayout>需要注意的是,textOn 與 textOff 必須與 android:showText="true" 一起使用才起作用。
•運行效果
•自定義Switch 控件textOn和textOff字體大小
設置 Switch 屬性的 android:switchTextAppearance="" 可以達到效果;
在 app/src/res 下找到 values 文件夾,右擊->New->Values Resource File,新建一個 style.xml 文件;
添加代碼如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name = "myTextAppearance" parent="@style/TextAppearance.AppCompat.Widget.Switch"> <item name="android:textSize">20sp</item> <item name="android:textColor">@color/teal_700</item> </style> </resources>在上述 <Switch> 控件中添加屬性 android:switchTextAppearance="@style/myTextAppearance" ;
運行效果: