我在google搜索了很多,并在stackoverflow中找到了一个如何使用样式和主题更改文本颜色的链接,但我不知道如何在代码中使用。
<style name="TextAppearance.Widget.IconMenu.Item" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">#ff0000</item>
</style>
请给我一些片段以便更好地理解。我知道如何通过使用Factory.In更改menuItem的背景,我们可以找到View对象。是否有任何设施可以获得菜单项。那我们可以改变menuItem的颜色吗?
答案 0 :(得分:2)
尝试使用此代码更改背景和文字颜色....
@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);
}
答案 1 :(得分:0)
我使用以下内容。将此方法放在您的活动中,并在onCreateOptionsMenu(菜单菜单)方法中调用它。 您可以设置backgroundResource而不是设置颜色...只需键入view.setbackground并通过自动完成查看可能性;)
/*
* IconMenuItemView is the class that creates and controls the options menu
* which is derived from basic View class. So We can use a LayoutInflater
* object to create a view and apply the background.
*/
protected void setMenuBackground() {
Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name
.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
/*
* The background gets refreshed each time a new item is
* added the options menu. So each time Android applies
* the default background we need to set our own
* background. This is done using a thread giving the
* background change as runnable object
*/
new Handler().post(new Runnable() {
public void run() {
view
.setBackgroundResource(R.drawable.row_blue_menu);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}
});
}
。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.start_menue, menu);
setMenuBackground();
return true;
}
答案 2 :(得分:0)
@ 0101100101只需将menu.IconmenutItem更改为menu.ActionMenuItemView