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

本文介绍如何在Android应用中自定义ActionMenu的Item样式,包括使用actionLayout属性改变字体颜色,解决margin无效问题,以及如何为TextView添加点击涟漪效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

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

 

上图为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了。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值