【问题标题】:Switch Track Color切换轨道颜色
【发布时间】:2014-10-01 10:08:57
【问题描述】:

我想使用 Switch 并更改其轨道颜色。所以在我看来没什么了不起的。

我的 Switch 布局:

<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>

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

我的错在哪里?

【问题讨论】:

  • 在 android:track="@android:color/darker_gray"上使用我的答案我认为你的@color/gray 有问题
  • 别忘了给我投票
  • 你能接受@Sector11 的回答吗,因为它只适用于任何情况。

标签: android colors widget android-switch


【解决方案1】:

除此之外没有什么对我有用

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);
                }

【讨论】:

  • 这对我很有帮助.. 谢谢@sector11
【解决方案2】:

以下代码解决问题:

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

你去...

希望这会对某人有所帮助?

【讨论】:

  • 谢谢老兄,这很好用,我们也可以自定义轨道颜色和拇指
  • 非常感谢,你拯救了我的一天!一个解释清楚的例子,能够自定义颜色和样式
【解决方案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>

【讨论】:

  • 简单干净。谢谢!
【解决方案4】:

你可能想用你的灰色构建一个drawable,比如并将它放在 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>

添加您想要的笔触大小(如果需要)并将此可绘制对象添加为轨道属性。

这有帮助吗?

【讨论】:

  • 我刚刚更新了我的答案。我试过了,它有效。它也应该对你有用。抱歉耽搁了。
【解决方案5】:

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

 android:track="@null"
 android:background="@drawable/img_setings_toggle_on"

【讨论】:

    【解决方案6】:
    android:track="@android:color/darker_gray"
    

    在你的代码中试试这个

    【讨论】:

    • 你的颜色名称和访问颜色不一样
    • 这使轨道变成了一个矩形:(
    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多