【问题标题】:Change Switch color in Navigation Drawer menu item在导航抽屉菜单项中更改开关颜色
【发布时间】:2019-03-05 22:33:38
【问题描述】:

我无法更改导航抽屉开关项目的颜色。我希望每个人都有一种与其动作相关的自定义颜色。

我用这样的菜单定义了我的导航抽屉:

<item
    android:title="@string/nav_drawer_services_title">
    <menu>
        <group android:checkableBehavior="none">
            <item
                android:id="@+id/Red_selector"
                android:title="@string/nav_drawer_red_title"
                app:actionLayout="@layout/switch_layout"/>
            <item
                android:id="@+id/Blue_selector"
                android:title="@string/nav_drawer_blue_title"
                app:actionLayout="@layout/switch_layout"/>
        </group>
    </menu>
</item>

我的 switch_layout.xml 是这样的:

<Switch
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="toggleService"
android:checkable="true"
android:checked="true"
style="@style/MyCustomSwitchTheme"
/>

我已尝试按照此处的建议更改 MyCustomSwitchThemeChange "on" color of a Switch,但切换项的颜色保持不变。

为什么我的自定义主题样式没有覆盖默认样式?我似乎无法从colors.xmlAppTheme 中定义的colorAccent 更改这些开关。

有人知道解决这个问题的方法吗?

编辑:这是样式代码,很抱歉没有将其包含在 OP 中。

<style name="MyCustomSwitchTheme">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">#46bdbf</item>

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

    <!-- inactive track color (30% transparency) -->
    <item name="android:colorForeground">#42221f1f</item>
</style>

【问题讨论】:

  • 我没有看到任何变色声明。请发布您的主题代码。

标签: android android-layout android-switch


【解决方案1】:

只需使用android:theme="@style/MyCustomSwitchTheme"

改为style="@style/MyCustomSwitchTheme"

【讨论】:

  • 哇...我很笨。 (或者至少今天盯着屏幕太久了。)我讨厌在发布问题之前忽略一些小错别字。不过谢谢,你是对的!
猜你喜欢
  • 2016-06-16
  • 2015-09-02
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多