处于关闭状态时,Android SwitchCompat无法呈现跟踪

时间:2016-04-16 17:28:30

标签: android switchcompat

我的问题可能最好用视觉方式询问 - 我想要一个SwitchCompat开关来查看他们在Android设置应用中的行为:

关闭:

enter image description here

这是:

enter image description here

但出于某种原因,我的SwitchCompat开关在关闭时看起来像这样:

enter image description here

没有灰色"轨道"延伸到右侧。但是,当打开时,它看起来像预期的那样:

enter image description here

如您所见,我已将自定义色调应用于我的应用程序。我的自定义色调应用如下:

<activity
    android:name=".editor.MySettingsEditor"
    android:theme="@style/Theme.MyCustomTheme" />

然后,在styles.xml中:

<style name="Theme.MyCustomTheme" parent="Theme.AppCompat">    
    <item name="colorAccent">@color/myColorAccent</item>
    <item name="colorPrimary">@color/myColorPrimary</item>
    <item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

为确保不是我的自定义样式导致此问题,我通过执行以下操作将其删除:

<activity
    android:name=".editor.MySettingsEditor"
    android:theme="@style/Theme.AppCompat" />

但是,&#34; off&#34;轨迹不显示,但色调颜色现在变为Android默认蓝绿色。

enter image description here enter image description here

为什么我的SwitchCompat开关在处于关闭状态时会丢失灰色轨道?

描述SwitchCompat的XML非常简单:

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

谢谢!

2 个答案:

答案 0 :(得分:5)

您可以通过为活动颜色设置colorControlActivated而为非活动颜色设置colorSwitchThumbNormal来应用自定义色调。如果您想更改曲目颜色,则可以设置android:colorForeground

<style name="CustomSwitch" parent="Theme.AppCompat">  
    <!-- active thumb-->
    <item name="colorControlActivated">@color/active_switch_color</item>

    <!-- inactive thumb-->
    <item name="colorSwitchThumbNormal">@color/inactive_switch_color</item>

    <!-- inactive track color -->
    <item name="android:colorForeground">@color/inactive_track_color</item>
</style>  

如果您想直接在视图中设置自定义主题:

<android.support.v7.widget.SwitchCompat
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:theme="@style/CustomSwitch"/>

答案 1 :(得分:1)

@esilver如果你想动态处理它们,也许是更好的方法

试试这个...我已经测试过,它的工作非常好

public class MainActivity extends AppCompatActivity {
    int[][] states = new int[][] {
            new int[] {-android.R.attr.state_checked},
            new int[] {android.R.attr.state_checked},
    };

    int[] thumbColors = new int[] {
            Color.BLACK,
            Color.RED,
    };

    int[] trackColors = new int[] {
            Color.GREEN,
            Color.BLUE,
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch);
        DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
        DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
    }
}

您只需要更新&#34; trackColors&#34;和#34; thumbColor&#34;根据您的要求/需求。

相关问题