安卓更改Menu的字体颜色(使用actionLayout,适用于多个fragment)

安卓更改Menu的字体颜色(使用actionLayout,适用于多个fragment)

与普通menu不一样的是,需要改变字体颜色的Item使用了app:actionLayou属性(使用android:actionLayout不起作用),个人测试当使用了这个属性,android:title将不会显示。

 

安卓更改Menu的字体颜色(使用actionLayout,适用于多个fragment)

上图为actionLayout的布局,使用padding是因为margin不起作用

 

安卓更改Menu的字体颜色(使用actionLayout,适用于多个fragment)

上图为fragment的代码,项目用的是kotlin,也不专门去写java了,when等价于switch,应该能看懂?

最后

给 TextView 设置点击涟漪效果,参考https://www.jianshu.com/p/60369e2ac5ca

  • 在 attrs.xml 中添加:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MaterialRipple">
        <attr name="viewItemBackground" format="reference" />
        <attr name="viewItemForeground" format="reference" />
        <attr name="viewItemForegroundBorderless" format="reference" />
    </declare-styleable>
</resources>
  • style 文件中添加;
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="viewItemBackground">?attr/selectableItemBackground</item>
    <item name="viewItemForeground">?attr/selectableItemBackground</item>
    <item name="viewItemForegroundBorderless">?attr/selectableItemBackgroundBorderless</item>
</style>
  • TextView 直接使用 android:background="?viewItemForegroundBorderless" 就OK了。