我想改变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
上面的setMenuBackground没有任何效果:
<img src="http://i.stack.imgur.com/xFlzp.png" width="240" alt="菜单样本">;
在上图中,我想把菜单背景从黑色改为行动栏中的蓝色。我怎样才能做到这一点呢,我做错了什么?
Sunny建议的动作栏样式生成器非常有用,但它会生成多的文件,如果你只想改变背景颜色,其中大部分是不相关的。
因此,我深入研究了它生成的压缩文件,并试图缩小哪些部分是重要的,这样我就可以对我的应用程序做最少的修改。以下是我发现的情况。
在样式生成器中,相关的设置是弹出窗口的颜色,它影响"溢出菜单、子菜单和旋转面板的背景"。
继续生成压缩包,但在所有生成的文件中,你只需要一张图片,menu_dropdown_panel_example.9.png
,它看起来像这样:
在此输入图片描述]4 !
所以,把它的不同分辨率版本添加到res/drawable-*
。(也许还可以把它们重命名为menu_dropdown_panel.9.png
。)
然后,作为一个例子,在res/values/themes.xml
中你会有以下内容,android:popupMenuStyle
和android:popupBackground
是关键设置。
<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
(Holo Dark),还需要一个额外的步骤,如本答案描述!
另外,如果你(像我一样)想为整个动作栏设计样式,而不仅仅是菜单,可以在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>
至少在安卓4.0以上版本(API级别14以上)上效果很好。
有一个简单的方法来改变ActionBar的颜色
使用ActionBar Generator并复制粘贴所有文件到你的res
文件夹,然后在Android.manifest文件中改变你的主题。