Android:SwitchCompat,填充和颜色问题

Ers*_*man 8 android switchcompat

我正在使用android.support.v7.widget.SwitchCompat,我遇到了以下问题

  • 我的样式包括colorControlActivated不适用
  • 使用Android命名空间和Res-Auto切换填充无效
  • 如何将拇指文本设置为全部大写

我的代码

Styles.xml

注意我试过没有父和Theme.AppCompat.Light.NoActionBar

<style name="ToggleSwitchStyle" parent="Theme.AppCompat">
    <item name="colorControlActivated">@color/emerald</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的SwitchCompat在XML布局中定义

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:padding="5dp"
    android:textOff="@string/no"
    android:textOn="@string/yes"
    app:showText="true"
    android:switchPadding="5dp"
    app:switchPadding="10dp"
    app:theme="@style/ToggleSwitchStyle"
    android:theme="@style/ToggleSwitchStyle"
    android:textAllCaps="true"
    app:thumbTextPadding="5dp"
    >
Run Code Online (Sandbox Code Playgroud)

所以在上面的文字中,AllCaps不会使拇指上的文字全部大写.

切换填充无效

使用Res-Auto或Android命名空间的主题对活动颜色没有影响.

但是,我可以通过更改材质主题的颜色重音来更改活动颜色

 <!-- Application theme. -->
    <style name="MaterialDesign" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/yellow</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

Pra*_*raj 1

确保您的 Styles.xml 位于 value-v21 文件夹中。我在 switchcompat 更改颜色时遇到了类似的问题。

这对我有用。将 styles.xml 保留在 value-v21 文件夹中,并使用 ColorAccent 来更改开关的颜色。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="colortoggle">
        <item name="colorAccent">@color/continue_button</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

对于拇指问题:textAppearance 和相关的 setTypeface() 方法控制标签文本的字体和样式,而 switchTextAppearance 和相关的 seSwitchTypeface() 方法控制拇指的字体和样式。