10

我想使用一个开关并改变它的轨道颜色。所以在我看来没什么了不起的。

我的开关布局:

<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 像素开关。这只是一条小线。如果我删除“颜色”行,则开关是正确的(当然没有灰色)。

我的错在哪里?

4

6 回答 6

17

除了这个,没有什么对我有用

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);
                }
于 2016-04-20T11:33:08.600 回答
14

下面的代码解决了这个问题:

在您的活动/片段(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>

给你...

希望这会对某人有所帮助

于 2018-11-26T10:29:42.430 回答
3

这是一个非常简单的解决方案,对我有用。只需使用颜色选择器设置 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>
于 2020-12-17T08:14:18.920 回答
2

你可能想用你的灰色构建一个drawable,比如并将它放在 res/drawable 文件夹中:

轨道.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>

添加您想要的任何尺寸(如果需要)并将此可绘制对象添加为轨道属性。

这有帮助吗?

于 2014-10-01T10:21:31.227 回答
1

试试这个来改变开关的轨道颜色

 android:track="@null"
 android:background="@drawable/img_setings_toggle_on"
于 2017-05-11T06:32:29.123 回答
-4
android:track="@android:color/darker_gray"

在你的代码中试试这个

于 2014-10-01T10:13:56.833 回答