android - 为 Material Design 设计 Android Lollipop 中的 SwitchCompat 按钮样式/着色

标签 android android-5.0-lollipop material-design android-switch

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

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

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

如果没有直接设置 Switch 颜色的方法,我可以用来制作自己的版本的可绘制对象位于何处?

最佳答案

I have been trying to find resources explaining as to how I can style switch button in the Material Design theme.

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

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

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

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

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

您为 colorAccent 指定的颜色将用于为应用中的小部件着色,例如 SwitchCompatsEditTextsRadioButtons

因此您的 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>

where are the drawables located that I can use to make my own version?

我不建议直接更改可绘制对象,但它们位于

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

关于android - 为 Material Design 设计 Android Lollipop 中的 SwitchCompat 按钮样式/着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356351/

相关文章:

android - 将文本从 Fragment 传递到 Fragment 到 Activity 到 Android Studio 中的接收位图

android - 如何从android studio链接和调用预构建静态 native 库的功能

android - FlexboxLayout - 在第二行android的末尾显示标签或芯片或标签计数

c# - WPF Material 设计凸起按钮悬停状态

android - 如何为 Android 构建传感器模拟器?

android - Xamarin Android Player 无法找到 VirtualBox - 我该如何解决?

android - TranslationZ 在 Android 中实际上做了什么?

android - SharedElement 在 Activity 转换中崩溃

android - java.lang.IllegalArgumentException : Service Intent must be explicit: Intent

css - Material 设计中的水平对齐元素