我想更改Android 4.2中选项(溢出)菜单的背景颜色 . 我已经尝试了所有方法,但它仍然显示主题设置的默认颜色 . 我使用了以下代码和XML配置 .
MainActivity.java
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setTitle("Sample Menu");
getActionBar().setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#33B5E5")));
int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView titleText = (TextView)findViewById(titleId);
titleText.setTextColor(Color.parseColor("#ffffff"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
setMenuBackground();
return true;
}
protected void setMenuBackground(){
// Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory( new Factory() {
@Override
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 () {
// sets the background color
view.setBackgroundResource( R.color.menubg);
// sets the text color
((TextView) view).setTextColor(Color.WHITE);
// sets the text size
((TextView) view).setTextSize(18);
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}});
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:icon="@drawable/menu"
android:showAsAction="always"
android:title="@string/action_settings">
<menu>
<item
android:id="@+id/item1"
android:showAsAction="always"
android:title="@string/item1" />
<item
android:id="@+id/item2"
android:showAsAction="always"
android:title="@string/item2" />
<item
android:id="@+id/item3"
android:showAsAction="always"
android:title="@string/item3" />
<item
android:id="@+id/item4"
android:showAsAction="always"
android:title="@string/item4" />
</menu>
</item>
</menu>
color.xml
<color name="menubg">#33B5E5</color>
The above setMenuBackground is not taking any effect:
在上图中, I want to change the menu background from black to the blue color in the Action Bar . 我怎样才能做到这一点,我做错了什么?
15 回答
有一种简单的方法可以更改操作栏中的颜色使用ActionBar Generator并复制粘贴
res
文件夹中的所有文件,并在Android.manifest文件中更改主题 .Action Bar Style Generator,suggested by Sunny非常有用,但它会生成 a lot 个文件,如果您只想更改背景颜色,则大多数文件都无关紧要 .
因此,我深入研究了它生成的zip,并试图缩小重要的部分,因此我可以对我的应用程序进行最少量的更改 . 以下是我发现的 .
在样式生成器中,相关设置为 Popup color ,这会影响“ Overflow menu ,子菜单和微调器面板背景” .
继续生成zip,但是在生成的所有文件中,你只需要一个图像, menu_dropdown_panel_example.9.png ,看起来像这样:
因此,将其不同的分辨率版本添加到
res/drawable-*
. (也许可以将它们重命名为menu_dropdown_panel.9.png
. )然后,作为示例,在
res/values/themes.xml
中,您将拥有以下内容,其中 android:popupMenuStyle 和 android:popupBackground 是关键设置 .当然,在
AndroidManifest.xml
:这个设置你得到了什么:
请注意,我使用 Theme.Holo.Light 作为基本主题 . 如果你使用 Theme.Holo (Holo Dark),还需要一个额外的步骤this answer describes!
此外,如果你(像我一样)想要设置整个Action Bar的样式,而不仅仅是菜单,请在_1545579中输入这样的内容:
至少在Android 4.0(API级别14)上运行良好 .
如果人们仍在访问工作解决方案,这对我有用: - 这适用于Appcompat支持库 . 这是ActionBar造型的延续explained here
以下是styles.xml文件 .
这就是它的外观 - MenuItem背景颜色是天蓝色,MenuItem文本颜色是粉红色,文本大小为25sp: -
如果您阅读本文,则's probably because all the previous answers didn'适用于基于 Holo Dark 的主题 .
Holo Dark为PopupMenus使用了一个额外的包装器,因此在执行what Jonik suggested之后,您必须将以下样式添加到'xml'文件中:
然后在主题块中引用它:
而已!
我在弹出菜单/选项菜单中更改文本背景颜色和颜色的简单方法
您可以按照以下方式在Overflow MenuItem中应用样式和主题 . OverFlow菜单是ListView所以,我们可以按照列表视图应用主题 .
在styles.xml中应用以下代码
如果你在android studio的最新工具栏中工作,那么这是最小的解决方案要更改工具栏选项菜单颜色,请将其添加到工具栏元素
然后在styles.xml中定义弹出菜单样式
<style name="MyDarkToolbarStyle" parent="ThemeOverlay.AppCompat.Light"> <item name="android:colorBackground">@color/mtrl_white_100</item> <item name="android:textColor">@color/mtrl_light_blue_900</item> </style>
请注意,您需要使用colorBackground而不是背景 . 后者将应用于所有内容(菜单本身和每个菜单项),前者仅适用于弹出菜单 .
我也遇到了同样的问题,最后我得到了简单的解决方案 . 刚添加一行到动作栏样式 .
"android:colorBackground" 足以更改选项菜单背景
我只需将十六进制值设置为:就能改变动作溢出的颜色:
快点!
然后将此特定样式添加到AppTheme样式中
DONE!
试试这个代码 . 将此代码段添加到res> values> styles.xml
在Manifest.xml中,在应用程序下添加以下代码段
将以下内容添加到
Styles.xml
仅在
activity_main.xml
中更改主题 .以下是主题中更改操作栏和溢出菜单背景颜色所需的更改 . 你需要配置actionBarStyle和popupMenuStyle的“android:background”
在您的应用主题中,您可以设置android:itemBackground属性以更改操作菜单的颜色 .
例如: