我在我的应用程序,并改变他们的颜色我用了一个自定义绘制选择的每个开关具有不同颜色的几个开关。
I have a few switches with different colors in my application and to change their colors I used a custom drawable selector for each switch.
在新的AppCompat V21库中发布有一个新的android.support.v7.widget.SwitchCompat控制。
When the new AppCompat v21 library was released there was a new android.support.v7.widget.SwitchCompat control.
是否有可能使用XML或code更改SwitchCompat的颜色,而无需使用可绘,例如?
Is it possible to change the color of a SwitchCompat without using drawables, for instance using XML or code?
首先,你应该看一看,以appCompat LIB文章there并以不同的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)
使用previous attributs您可以定义自己的主题,每个活动:
With previous attributs 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通过拦截任何布局通胀和插入部件的特殊色彩感知的版本,在它的地方小部件着色(见克里斯·巴内斯的后它),你不能自定义样式应用到您的布局XML文件中的每个开关。你必须设置一个自定义的背景下,将着色开关与正确的颜色。
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.
-
要做到这一点的 pre-5.0 您需要创建一个背景下,覆盖全球主题与海关attributs然后以编程方式创建开关:
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 它看起来像一个新的观点attribut来生活:安卓主题
(同为一体的用途清单中活动的声明)。基于另一个克里斯·巴内斯帖子,后者你应该能够定义一个直接自定义主题从布局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"/>
由于vine'th我完成我的答案有一个链接,这样,请回答,解释如何指定轨道的前景,当开关处于关闭状态,这是的有的。
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.