更改开关的“开启”颜色

229

我正在使用ICS应用程序中的holo.light主题和标准的Switch控件。

我想要将开关按钮的高亮或打开状态颜色从标准的浅蓝色更改为绿色。

这应该很容易,但我似乎无法弄清如何实现。


我真的没有看到其他的方法,除了复制相关平台资源(选择器和*.png文件)并修改“on”状态的可绘制对象。别忘了之后将Switch指向这个自定义选择器。 - MH.
2
对于任何搜索这个非常古老的问题的人来说,现在已经非常容易了,@مهند عطية的答案如下:https://dev59.com/Emgu5IYBdhLWcg3winnU#58362379 - Fattie
27个回答

12

可能有点晚了,但对于开关按钮来说,切换按钮不是答案,你必须更改开关的xml参数中的可绘制项:

 android:thumb="your drawable here"

10
这对我有用:
1. 在 values/styles.xml 中编写代码。
 <style name="SwitchTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorControlActivated">#148E13</item>
</style>

2. 在您的布局文件的switch中添加以下代码行:

android:theme="@style/SwitchTheme"

最好且简单的解决方案 - Betini O. Heleno

9
在Android Lollipop及以上版本中,您可以在主题样式中定义它:
<style name="BaseAppTheme" parent="Material.Theme">
    ...
    <item name="android:colorControlActivated">@color/color_switch</item>
</style>

这个解决方案的问题在于它也改变了我选择的文本编辑器的颜色,这不是我想要的。 - codewing
@codewing 当然会。因为这是设置在全局样式偏好中的。为了实现你的目标,需要找到目标视图的视图ID并修改其属性。 - emen
好的,那是针对“开启”位置的。那么对于“关闭”位置改变拇指颜色怎么办? - Steven

6

2
是的,可以这样做,但肯定有更简单的方法吧?选择器可能可以吗? - JamesSugrue
不,没有更简单的方法。你不能神奇地改变一张图片的颜色 :) - you786
好观点。直到我深入了解之前,我并没有完全理解它是如何工作的。 - JamesSugrue
@Denizen,你确定吗?如果是的话,请发表答案,我会投票支持它。 - you786
@Denizen 请删除您的评论。Android Switch 始终返回 null 以获取 getBackground(),我浪费了 30 分钟尝试这个。 - Shirish Herwade

5

arlomedia提出的解决方案对我有用。关于他的额外空间问题,我通过将所有填充删除到开关上来解决。

编辑

根据要求,这是我的内容。

在布局文件中,我的开关位于线性布局内,并在TextView之后。

<LinearLayout
        android:id="@+id/myLinearLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal|center"
        android:gravity="right"
        android:padding="10dp"
        android:layout_marginTop="0dp"
        android:background="@drawable/bkg_myLinearLayout"
        android:layout_marginBottom="0dp">
        <TextView
            android:id="@+id/myTextForTheSwitch"
            android:layout_height="wrap_content"
            android:text="@string/TextForTheSwitch"
            android:textSize="18sp"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal|center"
            android:gravity="right"
            android:layout_width="wrap_content"
            android:paddingRight="20dp"
            android:textColor="@color/text_white" />
        <Switch
            android:id="@+id/mySwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="@string/On"
            android:textOff="@string/Off"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_toRightOf="@id/myTextForTheSwitch"
            android:layout_alignBaseline="@id/myTextForTheSwitch"
            android:gravity="right" />
    </LinearLayout>

自从我开始使用Xamarin / Monodroid(最低Android 4.1),我的代码如下:

Android.Graphics.Color colorOn = Android.Graphics.Color.Green;
Android.Graphics.Color colorOff = Android.Graphics.Color.Gray;
Android.Graphics.Color colorDisabled = Android.Graphics.Color.Green;

StateListDrawable drawable = new StateListDrawable();
drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn));
drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled));
drawable.AddState(new int[] { }, new ColorDrawable(colorOff));

swtch_EnableEdit.ThumbDrawable = drawable;

swtch_EnableEdit之前是这样定义的(Xamarin):

Switch swtch_EnableEdit = view.FindViewById<Switch>(Resource.Id.mySwitch);

我完全没有设置内边距,也没有调用`.setPadding(0, 0, 0, 0)`。

你能提供相关代码吗?我已经尝试使用 switchInput.setPadding(0, 0, 0, 0) 但是没有任何改变。 - arlomedia
1
使用AppCompat库在Xamarin中进行测试,以针对5.x版本,结果如广告所述。 - Alex.Ritna

5
最简单的方法是定义跟踪色调,并将色调模式设置为src_over以消除30%的透明度。
android:trackTint="@drawable/toggle_style"
android:trackTintMode="src_over"

toggle_style.xml

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

4

您可以为开关小部件制作自定义样式,当对其进行自定义样式时,可以使用颜色强调作为默认值。

<style name="switchStyle" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>    <!-- set your color -->
</style>    

3

1

我不知道如何从Java中实现,但如果您为应用程序定义了样式,您可以在样式中添加此行代码,这样您就可以获得所需的颜色,对于我来说,我使用了#3F51B5。

<color name="ascentColor">#3F51B5</color>

1

在这里尝试找到正确的答案:TextView背景颜色选择器。 用XML创建一个带有颜色的形状,然后将其分配给你选择器中的"checked"状态。


不错的解决方案,但您知道如何动态更改颜色吗? - inverted_index
你只需要将选择器设置为背景资源即可。 - Shamm

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