首页 文章

在Android Lollipop for Material Design中为SwitchCompat按钮设置样式/着色

提问于
浏览
5

我一直在努力寻找资源,解释如何在Material Design主题中设置 Switch 按钮的样式 .

此链接确实解释了颜色值和美学细节,但没有说明如何通过在Material设计主题中设置某些属性来实现此目的 .

http://www.google.com/design/spec/components/switches.html#switches-switch

如果没有直接的方法来设置Switch的颜色,那么我可以使用哪些drawable来制作我自己的版本?

3 回答

  • 17

    我一直在尝试查找资源,解释如何在Material Design主题中设置切换按钮的样式 .

    现在使用新的appcompat-v7:21,着色小部件非常简单 .

    只要您使用appcompat-v7:21,就可以用 SwitchCompat 小部件替换所有旧的 Switch 小部件 . 因此,在xml布局中,使用 android.support.v7.widget.SwitchCompat 而不是使用旧的 Switch 标记 .

    然后在styles.xml中,确保您应用的父主题是 Theme.AppCompat 主题,例如 Theme.AppCompat.Light .

    最后,关键是为 colorAccent 指定自己的值:

    <item name="colorAccent">@color/my_fancy_color</item>
    

    The color you specify for colorAccent will be used to color the widgets in your app such as SwitchCompats, EditTexts, RadioButtons, etc.

    所以你的styles.xml可能看起来像:

    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/color_primary</item>
    
        <!-- colorPrimaryDark is used to color the status bar -->
        <item name="colorPrimaryDark">@color/color_primary_dark</item>
    
        <!-- 
             colorAccent is used as the default value for colorControlActivated
             which is used to color widgets 
        -->
        <item name="colorAccent">@color/my_fancy_color</item>
    
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight & colorSwitchThumbNormal. -->
    </style>
    

    我可以使用哪些drawables来制作我自己的版本?

    我不建议直接改变drawable,但它们位于

    sdk/platforms/android-21/data/res/drawable-XXXX

    并调用文件

    btn_switch_to_off_mtrl_XXXXX.9.png

    btn_switch_to_on_mtrl_XXXXX.9.png

    switch_track_mtrl_alpha.9.png

  • 2

    完成JDJ的答案:

    AppCompat中的drawable-hdpi中存在损坏文件的错误:

    https://code.google.com/p/android/issues/detail?id=78262

    要修复它,只需使用这两个文件覆盖它:

    https://github.com/lopespm/quick-fix-switchcompat-resources

    将它们添加到drawable-hdpi目录中 .

    XML

    <android.support.v7.widget.SwitchCompat
    android:id="@+id/dev_switch_show_dev_only"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    

    Java中没有必要

  • 6

    在我的情况下,我只想设置一个特定的开关,而不是应用程序中的所有开关 . 以下是我使用AppCompat-v7:23的方法

    xml布局:

    <android.support.v7.widget.SwitchCompat
                    android:id="@+id/switchAffiliateMember"
                    android:theme="@style/Sugar.Switch.Affiliate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:textOff="No"
                    android:textOn="Yes" />
    

    V21 / styles.xml:

    <style name="Sugar.Switch.Affiliate" parent="Base.Widget.AppCompat.CompoundButton.Switch">
        <item name="colorSwitchThumbNormal">@color/red</item>
        <item name="colorAccent">@color/white</item>
    </style>
    

    colorSwitchThumbNormal是“off”状态,colorAccent是“on”状态 . 请注意,这些都没有“android”命名空间前缀,我不明白为什么,但它只适用于我这种方式 .

相关问题