关于使用 ActionBarSherlock 或其他库更改硬键菜单的文本颜色或背景图像存在很多问题。但我找不到任何使用 AppCompat 与 Material 主题配合使用的答案。
那么如何更改硬键菜单面板的文本颜色和背景?
默认情况下,面板如下所示(Light.DarkActionBar 主题)。我真的不喜欢丑陋的灰色到灰色的对比,我想将文本颜色更改为黑色。
最佳答案
AppCompat 库使用 Theme.AppCompat.CompactMenu
作为硬键菜单。默认情况下,硬键菜单始终为灰色,无论您使用浅色还是深色主题都没有关系。
默认 9 block 背景:
深入研究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>
设置背景样式
一旦您知道如何做,设置背景样式实际上非常简单。您只需将 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>
设置文本样式
文本样式有点复杂。您需要创建一个自定义 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>
然后将您自定义创建的 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>
结果
(此示例图像中的背景属性未更改)
关于android - 使用 AppCompat 和 Material 更改硬键菜单面板的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367206/