Android Studio中Switch控件有關 textOn 和 textOff 用法


 

•屬性

  • 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" ;

  運行效果:

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM