Customizing Background and Text color in Options Menu Android
2012-06-15 10:11
656 查看
Tweet
6
Options menu in android can be customized to set the background or change the text appearance. The background and text color in the menu couldn’t be changed using themes and styles.
The android source code (datareslayouticon_menu_item_layout.xml)uses a custom item of class “com.android.internal.view.menu.IconMenuItem”View for the menu layout. We can make changes in the above class to customize the menu. To achieve the same, use LayoutInflater
factory class and set the background and text color for the view.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// set the background drawable
view .setBackgroundResource(R.drawable.my_ac_menu_background);
// set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
相关文章推荐
- How to change the Text color of Menu item in Android?
- Coloring the Console Application (AddingText and Background color in C#)
- Setting the background color and background alpha on a Flex PopUpButton control’s pop up menu
- android setTextColor setImageResource setBackgroundResource
- android设置toolbar的menu itemTextView颜色 set toolbar menu item text color
- Controls over the EditText and the IME in Android
- How to disable cursor positioning and text selection in an EditText? (Android)
- Set terminal text color and transparent background
- 【Android基础】setTextColor(),setBackgroundColor的参数设置方式
- Is there any way to define a min and max value for edittext in android?
- 【Android基础】setTextColor(),setBackgroundColor的参数设置方式
- Customizing your button or TextView or another view in ANDROID.
- (Sharepoint 2010) Change the background and font color of text for a web part header
- Change the Foreground and Background Colour of Staic Text Control in VC++
- Customizing the Options Menu Background
- Android进阶之OptionsMenu+ContextMenu+subMenu
- android 设置各种颜色 android:background="@color/white"
- Android使用onCreateOptionsMenu()创建菜单Menu的方法详解
- android项目从零开始 TextView setTextColor等背景选择器设置失败问题