使用AppCompat和Material更改硬键菜单面板的样式

Rol*_*f ツ 2 android android-appcompat android-menu android-theme material-design

关于使用ActionBarSherlock或其他库更改硬键菜单的文本颜色或背景图像有很多问题.但我找不到任何与使用AppCompat的Material主题相关的答案.

那么如何更改硬键菜单面板的文本颜色和背景?

默认情况下,面板看起来像这样(Light.DarkActionBar主题).我真的不喜欢丑陋的灰色到灰色的对比,我想把文字颜色改成黑色.

默认菜单看

Rol*_*f ツ 5

AppCompat库使用Theme.AppCompat.CompactMenu硬键菜单.硬键菜单始终为灰色,默认情况下,如果您使用浅色或深色主题则不会显示.

默认9补丁背景:

硬键菜单的默认9补丁图像

深入研究AppCmpat源代码时,基本compat主题(Base.V7.Theme.AppCompat)中使用了几个属性来设置硬键菜单的样式:

<item name="panelMenuListWidth">@dimen/abc_panel_menu_list_width</item>
<item name="panelMenuListTheme">@style/Theme.AppCompat.CompactMenu</item>
<item name="panelBackground">@drawable/abc_menu_hardkey_panel_mtrl_mult</item>
<item name="android:panelBackground">@android:color/transparent</item>
<item name="listChoiceBackgroundIndicator">@drawable/abc_list_selector_holo_dark</item>
Run Code Online (Sandbox Code Playgroud)

造型背景

一旦你知道如何做背景,那么设计背景实际上非常简单.您只需将panelBackground属性添加到自定义应用主题即可.

<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="panelBackground">@drawable/yourCustomBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)

样式文本

样式化文本有点复杂.您需要创建自定义panelMenuListTheme并设置itemTextAppearance它.

<style name="Theme.YourTheme.CompactMenu" parent="@style/Theme.AppCompat.CompactMenu">
    <item name="android:itemTextAppearance">@style/TextAppearance.YourTheme.Material.CompactMenu</item>
</style>

<style name="TextAppearance.YourTheme.Material.CompactMenu" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/yourCustomColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后将自定义创建的panelMenuListTheme主题添加到自定义应用主题:

<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="panelMenuListTheme">@style/Theme.YourTheme.CompactMenu</item>
</style>
Run Code Online (Sandbox Code Playgroud)

结果

(此示例图像中的background属性未被更改)

造型后的菜单