安卓 popmenu菜单弹出框 item字体颜色怎么设置

一般常规都是在对应的activity 设置主题风格 :

eg:

<style name="AppTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/MyTextAppearance</item>
    <item name="android:textAppearanceLargePopupMenu">@style/MyTextAppearance</item>
</style>

<style name="MyPopupMenu" >
    <item name="android:popupBackground">@mipmap/bg_tanchuang</item>
    <item name="android:layout_marginTop">10dp</item>
</style>

<style name="MyTextAppearance">
    <item name="android:textColor">@color/item_text_color_selector</item>
    <item name="android:textSize">22sp</item>
</style>

但是我有一个需求 是这样的

会改变item中的字体颜色 我在style中设置 字体颜色选择xml文件 没有效果 后面想到了用 SpannableString这个api去实现颜色的替换 完美解决了需求 show me the code :

final PopupMenu pop = new PopupMenu(this, view);
pop.getMenuInflater().inflate(R.menu.main, pop.getMenu());
if (selectItem == 0) {
    SpannableString ss = new SpannableString(getResources().getString(R.string.thumbnail_big));
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#00F0FF"));
    ss.setSpan(foregroundColorSpan, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    pop.getMenu().findItem(R.id.action_big).setTitle(ss);
} else if (selectItem == 1) {
    SpannableString ss = new SpannableString(getResources().getString(R.string.thumbnail_middle));
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#00F0FF"));
    ss.setSpan(foregroundColorSpan, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    pop.getMenu().findItem(R.id.action_middle).setTitle(ss);
} else if (selectItem == 2) {
    SpannableString ss = new SpannableString(getResources().getString(R.string.thumbnail_small));
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#00F0FF"));
    ss.setSpan(foregroundColorSpan, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    pop.getMenu().findItem(R.id.action_small).setTitle(ss);
}

红色标记的部分就是设置item 字体颜色了~

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值