转换轨道颜色

14

我想使用一个开关并更改它的轨道颜色。在我看来,这没有什么特别的。

我的开关布局:

<Switch
    android:id="@+id/Switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:track="@color/gray"
    android:textOn="@string/on"
    android:textOff="@string/off"     
    android:text="@string/musiconoff" />

以及我的颜色"灰色":

<color name="gray">#666666</color>

我的问题是开关显示为一个1像素的开关,只是一个小线条。如果我删除“color”行,则开关是正确的(当然没有灰色)。

我的哪里出了错?


请使用我的答案:android:track="@android:color/darker_gray"。我认为你的@color/gray存在问题。 - Spry Techies
别忘了给我投票。 - Spry Techies
你能接受@Sector11的答案吗?因为这是唯一在任何情况下都有效的答案。 - TapanHP
6个回答

18

除了这个,没有其他东西对我有用。

if (isChecked) {
                    mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN);
                } else {
                    mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN);
                }

1
这对我非常有帮助。谢谢@sector11。 - TapanHP

16

以下代码可解决问题:

在你的Activity/Fragment(XML)中:

<Switch
    android:id="@+id/sMuteNotifications"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_margin="1dp"
    android:checked="true"
    android:gravity="center_vertical"
    android:switchMinWidth="56dp"
    android:thumb="@drawable/bg_thumb"       //  Create this drawable
    android:track="@drawable/bg_switch_states"/>       //  and this one too

bg_thumb:(使用此图片或其他任何你想要的图片)

在你的drawables文件夹中创建它

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="@android:color/white"/>

    <size android:width="28dp" android:height="28dp"/>

</shape>

bg_switch_states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:drawable="@drawable/bg_on_switch"/>
    <item android:state_checked="false" android:drawable="@drawable/bg_off_switch"/>

</selector>

bg_on_switch :

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="18dip" />
    <solid android:color="@color/colorPrimaryLight"/>  // <---What ever color you want to use here
</shape>
bg_off_switch:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="18dip" />
    <solid android:color="@color/colorPrimaryDark"/>  // <---What ever color you want to use here
</shape>

就是这样了...

希望这能对某人有所帮助。


谢谢,伙计,这个完美地运作了,我们可以自定义轨道颜色和拇指。 - Venkatesh
非常感谢,你救了我的一天!一个解释得很好的示例,具有自定义颜色和样式的能力。 - Mo Zaatar
请使用app:track="@drawable/bg_switch_states"代替android:track="@drawable/bg_switch_states"。 - Abhinav Saxena

9

这里有一个非常简单的解决方案,适用于我。只需使用颜色选择器设置xml属性thumbTint和trackTint。Track是背景组件,thumb是圆形组件。

<com.google.android.material.switchmaterial.SwitchMaterial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:thumbTint="@color/switch_thumb"
        app:trackTint="@color/switch_track" />

或者更好的方法是将这些项添加到应用程序主题中,以应用于所有开关:

<item name="thumbTint">@color/switch_thumb</item>
<item name="trackTint">@color/switch_track</item>

switch_thumb.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/[your desired color]" android:state_checked="true" />
     <item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>

switch_track.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/[your desired color]" android:state_checked="true" />
     <item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>

简单而清晰。谢谢! - arun

2
你可能想要使用灰色构建一个可绘制对象,比如并将其放置在res/drawable文件夹中:
track.xml:
<shape 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">

     <size android:width="48dp" android:height="22dp" />
     <solid
         android:color="#666666" />
     <stroke
        android:width="2dp"
        android:color="#666666"
        android:dashWidth="2dp"
        android:dashGap="1dp" />
</shape>

如果需要,可以添加所需的描边大小,然后将此可绘制对象作为轨道属性添加。

这有帮助吗?


我刚刚更新了我的答案。我试过了,它有效。它应该对你也是有效的。抱歉让你等这么久。 - Mike

1
尝试这个来改变开关的轨道颜色:
 android:track="@null"
 android:background="@drawable/img_setings_toggle_on"

-4
android:track="@android:color/darker_gray"

在你的代码中试试这个


你的颜色名称和访问颜色不一致。 - Spry Techies
这将轨道变成一个矩形 :( - Mo Zaatar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接