android中的menu的背景颜色如何设置?请指教

yuanjh2001 2010-09-14 04:32:15
android中的menu背景是默认白色的,老板想改成其他颜色的,请各位指点一下,我试了,搞不定,好像要重写LayoutInflater.Factory,但是细节不是太清楚,请指点。
...全文
3666 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shun_qizi_ran 2010-10-21
  • 打赏
  • 举报
回复
楼上的高明,呵呵
leonplion 2010-09-25
  • 打赏
  • 举报
回复
这样可以实现自定义背景,但是聚焦时颜色也不改变了!有点郁闷……

/*
*The Options Menu (the one that pops up on pressing the menu button on the emulator)
* can be customized to change the background of the menu
*/

package com.pop.menu;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.LayoutInflater.Factory;

public class Options_Menu extends Activity {

private static final String TAG = "DEBUG";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

/* Invoked when the menu button is pressed */

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater inflater = new MenuInflater(getApplicationContext());
inflater.inflate(R.menu.options_menu, menu);
setMenuBackground();
return true;
}

/*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() {

@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 () {
view.setBackgroundResource( R.drawable.background);
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}
});
}
}
yuanjh2001 2010-09-14
  • 打赏
  • 举报
回复
你的方法好像不行,我试了一下,还是不可以
dinjay 2010-09-14
  • 打赏
  • 举报
回复
....

  <item android:id="@+id/happy"
android:title="Happy"
android:icon="@drawable/stat_happy"
android:background="#66FFFFFF"/>



background就是颜色
yuanjh2001 2010-09-14
  • 打赏
  • 举报
回复
1楼说的对,不过我想要做的是要把menu背景颜色自己定义一下,就是不知道怎么做?
dinjay 2010-09-14
  • 打赏
  • 举报
回复
参考
http://topic.csdn.net/u/20100730/09/0800bfa2-8f10-4a43-9c67-500faa9e1d9b.html
menu中每项都是xml中定义出来的。
可以添加
<item android:id="@+id/happy"
android:title="Happy"
android:icon="@drawable/stat_happy" />

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧