我想更改Android 4.2中选项(overflow)菜单的背景颜色。我已经尝试了所有的方法,但它仍然显示由主题设置的默认颜色。我使用了以下代码& 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>
上面的setMenuBackground没有起任何作用:
在上图中,我想在操作栏中将菜单背景从黑色更改为蓝色。我如何才能做到这一点,我做错了什么?
发布于 2013-10-29 21:36:43
有一个简单的方法来改变颜色在动作栏,使用ActionBar Generator和复制,粘贴所有文件在您的res
文件夹和更改您的Android.manifest文件中的主题。
发布于 2015-09-01 13:45:26
如果人们仍然在访问一个有效的解决方案,这里是对我有效的:--这是为Appcompat支持库。这是ActionBar styling explained here的延续
以下是styles.xml文件。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- This is the styling for action bar -->
<item name="actionBarStyle">@style/MyActionBar</item>
<!--To change the text styling of options menu items</item>-->
<item name="android:itemTextAppearance">@style/MyActionBar.MenuTextStyle</item>
<!--To change the background of options menu-->
<item name="android:itemBackground">@color/skyBlue</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@color/red</item>
<item name="titleTextStyle">@style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/red</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">25sp</item>
</style>
</resources>
这是它的外观--菜单项目背景颜色是天蓝色,MenuItem文本颜色是粉色,文本大小为25sp:--
发布于 2013-11-20 00:53:40
该,suggested by Sunny,是非常有用的,但它生成的很多的文件,其中大多数是无关的,如果你只想改变背景颜色。
因此,我更深入地挖掘了它生成的zip,并试图缩小重要部分的范围,这样我就可以对我的应用程序进行最少的更改。下面是我的发现。
在样式生成器中,相关的设置是Popup color,它会影响"Overflow menu,submenu spinner background“。
继续生成压缩文件,但是在生成的所有文件中,您实际上只需要一个图像menu_dropdown_panel_example.9.png
,,它看起来像这样:
因此,将它的不同分辨率版本添加到res/drawable-*
。(也许可以将它们重命名为menu_dropdown_panel.9.png
。)
然后,作为示例,在android:popupMenuStyle
和android:popupBackground
是关键设置的情况下,在res/values/themes.xml
中会有以下内容。
<resources>
<style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
<item name="android:actionBarStyle">@style/MyApp.ActionBar</item>
</style>
<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
</style>
<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<!-- Blue background color & black bottom border -->
<item name="android:background">@drawable/blue_action_bar_background</item>
</style>
</resources>
当然,在AndroidManifest.xml
中
<application
android:theme="@style/MyAppActionBarTheme"
... >
通过此设置,您可以获得以下结果:
请注意,我使用Theme.Holo.Light
作为基本主题。如果你使用Theme.Holo
(全息黑暗),有一个额外的步骤需要作为this answer describes!
另外,如果你(像我一样)想设计整个操作栏的样式,而不仅仅是菜单,在res/drawable/blue_action_bar_background.xml
中放入类似这样的内容:
<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF000000" />
<solid android:color="#FF2070B0" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#FF2070B0" />
<solid android:color="#00000000" />
<padding android:bottom="2dp" />
</shape>
</item>
</layer-list>
至少在Android 4.0+ (API级别的14+)上效果很好。
https://stackoverflow.com/questions/19659637
复制相似问题