如何自定义开关按钮?自定义、按钮

2023-09-13 00:12:15 作者:森屿暮歌

我期待为自定义的开关按钮成为如下:

I am looking to Custom The Switch Button to becoming as following :

如何实现这一目标?

推荐答案

您可以使用下面的code改变的颜色和文字

you can use the following code to change color and text :

<org.jraf.android.backport.switchwidget.Switch
                        android:id="@+id/th"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:thumb="@drawable/apptheme_switch_inner_holo_light"
                        app:track="@drawable/apptheme_switch_track_holo_light"
                        app:textOn="@string/switch_yes"
                        app:textOff="@string/switch_no"
                        android:textColor="#000000"
                        />

中创建一个名为水库XML colors.xml /值文件夹:

Create a xml named colors.xml in res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
</resources>

在绘制文件夹中,创建一个XML文件my_btn_toggle.xml:

In drawable folder, create a xml file my_btn_toggle.xml:

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@color/red"  />
        <item android:state_checked="true" android:drawable="@color/green"  />
    </selector>

在XML部分定义的切换按钮,地址:

and in xml section defining your toggle button add:

android:background="@drawable/my_btn_toggle

要改变的颜色纹元 textOff

android:switchTextAppearance="@style/Switch"