如何将switch'textOn / Off更改为轨道而不是拇指

时间:2016-04-26 12:47:20

标签: android

enter image description here

我想在轨道上设置文字,而不是在拇指之内。如何定制?

1 个答案:

答案 0 :(得分:0)

我采取了不同的方式来解决我的所有Switch相关问题。这是我的代码     

    <RadioButton
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:id="@+id/search"
        android:background="@drawable/toggle_widget_background"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:gravity="center"
        android:text="Search"
        android:textColor="@color/white" />

    <RadioButton
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:id="@+id/offer"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/toggle_widget_background"
        android:button="@null"
        android:gravity="center"
        android:text="Offers"
        android:textColor="@color/white" />
</RadioGroup>

pink_out_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80000000" />
<stroke
    android:width="1dp"
    android:color="@color/pink" />
</shape>

toggle_widget_background.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/pink" android:state_checked="true" />
    <item android:drawable="@color/dark_pink"     android:state_pressed="true" />
    <item android:drawable="@color/transparent" />
</selector>