如何从AppCompat库更改SwitchCompat的颜色 [英] How to change the color of a SwitchCompat from AppCompat library

查看:156
本文介绍了如何从AppCompat库更改SwitchCompat的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些带有不同颜色的开关控件,为了更改它们的颜色,我使用了多个自定义可绘制选择器.

I have a few switch controls with different colors in my application and to change their colors I used multiple custom drawable selectors.

随着AppCompat v21库的发布,引入了新的android.support.v7.widget.SwitchCompat控件.

A new android.support.v7.widget.SwitchCompat control was introduced with the release of the AppCompat v21 library.

是否可以通过编程方式在没有客户可绘制选择器的情况下通过XML或代码来更改SwitchCompat的颜色?

Is it possible to change the color of a SwitchCompat programmatically without customer drawable selector, but with XML or code?

推荐答案

AppCompat着色属性:

首先,您应该看一下appCompat lib文章,您可以设置其他属性:

AppCompat tinting attributs:

First, you should take a look to appCompat lib article there and to different attributs you can set:

colorPrimary :应用的主要品牌颜色.默认情况下,这是应用于操作栏背景的颜色.

colorPrimary: The primary branding color for the app. By default, this is the color applied to the action bar background.

colorPrimaryDark :主要品牌颜色的深色变体.默认情况下,这是应用于状态栏(通过statusBarColor)和导航栏(通过navigationBarColor)的颜色.

colorPrimaryDark: Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor).

colorAccent :对主要品牌颜色的明亮补充.默认情况下,这是应用于框架控件的颜色(通过colorControlActivated).

colorAccent: Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated).

colorControlNormal :在正常状态下应用于框架控件的颜色.

colorControlNormal: The color applied to framework controls in their normal state.

colorControlActivated :处于激活状态(例如,选中并打开)应用于框架控件的颜色.

colorControlActivated: The color applied to framework controls in their activated (ex. checked, switch on) state.

colorControlHighlight :应用于框架控件突出显示的颜色(例如波纹,列表选择器).

colorControlHighlight: The color applied to framework control highlights (ex. ripples, list selectors).

colorButtonNormal :在正常状态下应用于框架按钮的颜色.

colorButtonNormal: The color applied to framework buttons in their normal state.

colorSwitchThumbNormal :在正常状态下应用于框架开关拇指的颜色. (关闭)

colorSwitchThumbNormal: The color applied to framework switch thumbs in their normal state. (switch off)

使用先前的属性,您可以为每个活动定义自己的主题:

With previous attributes you can define your own theme for each activity:

<style name="Theme.MyActivityTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_awesome_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/my_awesome_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated,
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight, and colorSwitchThumbNormal. -->

</style>

和:

<manifest>
...
    <activity
        android:name=".MainActivity" 
        android:theme="@style/Theme.MyActivityTheme">
    </activity>
...
</manifest>


如果您想在单个活动中使用不同的自定义开关:

appcompat中的小部件着色可以通过拦截任何布局膨胀并在其位置插入小部件的特殊色觉版本来实现(请参阅Chris Banes


If you want to have differents custom switches in a single activity:

As widget tinting in appcompat works by intercepting any layout inflation and inserting a special tint-aware version of the widget in its place (See Chris Banes post about it) you can not apply a custom style to each switch of your layout xml file. You have to set a custom Context that will tint switch with right colors.

-

要在 5.0之前的版本中执行此操作,您需要创建一个上下文,该上下文将全球主题与海关属性一起覆盖,然后以编程方式创建您的开关:

To do so for pre-5.0 you need to create a Context that overlays global theme with customs attributs and then create your switches programmatically:

ContextThemeWrapper ctw = ContextThemeWrapper(getActivity(), R.style.Color1SwitchStyle); 
SwitchCompat sc = new SwitchCompat(ctw)

从AppCompat v22.1开始,您可以使用以下XML将主题应用于开关小部件:

As of AppCompat v22.1 you can use the following XML to apply a theme to the switch widget:

<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...>

    <android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:theme="@style/Color1SwitchStyle"/>

您的自定义开关主题:

<style name="Color1SwitchStyle">
    <item name="colorControlActivated">@color/my_awesome_color</item>
</style>

-

Android 5.0 上,似乎有了一种新的视图属性:android:theme(与清单中活动声明的一种用法相同).根据另一位Chris Banes的帖子,对于后者,您应该能够直接在布局xml的视图上定义自定义主题:

On Android 5.0 it looks like a new view attribut comes to life : android:theme (same as one use for activity declaration in manifest). Based on another Chris Banes post, with the latter you should be able to define a custom theme directly on a view from your layout xml:

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/Color1SwitchStyle"/>


更改SwitchCompat的轨道颜色

感谢vine'th,我以指向SO答案的链接来完成我的答案,该链接解释了如何在关闭开关的情况下指定轨道的前景,这是


To change the track color of a SwitchCompat

Thanks to vine'th I complete my answer with a link to SO answer that explains how to specify the Foreground of the Track when Switch is Off, it's there.

这篇关于如何从AppCompat库更改SwitchCompat的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆