简体   繁体   English

更改Android工具栏中复选框菜单项的颜色

[英]Change Color of checkbox menu item in Android toolbar

How do I change the color of an un checked menu item in a toolbar's submenu? 如何更改工具栏子菜单中选中菜单项的颜色?

I know that the checked state is defined by the accentColor of the corresponding theme. 我知道检查状态是由相应主题的accentColor的。 But I couldn't find a way to define a color for the unchecked state. 但我找不到为未经检查的状态定义颜色的方法。

带检查的工具栏子菜单 Just to be clear: I can't define a toolbar's menu item using a custom layout XML and I do not have direct access to the view object and the checkbox. 需要说明的是:我无法使用自定义布局XML定义工具栏的菜单项,而且我无法直接访问视图对象和复选框。

A menu like this is defined like so: 像这样的菜单定义如下:

<menu>
        <item
                android:id="@+id/sortByDescriptionDescendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDescriptionDescendingAction"
                android:checkable="true"/>
        <item
                android:id="@+id/sortByDescriptionAscendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDescriptionAscendingAction"
                android:checkable="true"/>
        <item
                android:id="@+id/sortByDateDescendingAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDateDescendingAction"
                android:checkable="true"
                android:checked="true"/>
        <item
                android:id="@+id/sortByDateDescAction_mediumDark"
                android:icon="@drawable/ic_sort_white_24dp"
                android:title="@string/sortByDateAscendingAction"
                android:checkable="true"/>
</menu>

Try using R.attr of android: see this 尝试使用Android的R.attr:看到这个

in your values/styles.xml: 在您的values / styles.xml中:

Define custom style for toolbar : toolbar定义自定义样式:

  <style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">  
    <item name="android:colorControlActivated">@color/BLUE</item>
    <item name="android:colorControlHighlight">@color/BLUE</item>
    <item name="android:colorControlNormal">@color/white</item>
</style> 

Apply it using: 使用以下方法:

<android.support.v7.widget.Toolbar  
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/CustomPopupTheme" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM