安卓 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 字体颜色了~