android 菜单背景颜色,更改android菜单的背景颜色

Abhay Kumar..

11

当你给菜单充气时,调用这个setMenuBackground()方法

@Override

public boolean onCreateOptionsMenu(Menu menu)

{

MenuInflater inflater=getMenuInflater();

inflater.inflate(R.menu.menu,menu);

setMenuBackground();

return true;

}

并在setMenuBackground()方法中写这个

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

// sets the background color

view.setBackgroundResource( R.color.androidcolor);

// sets the text color

((TextView) view).setTextColor(Color.BLACK);

// sets the text size

((TextView) view).setTextSize(18);

}

} );

return view;

}

catch ( InflateException e ) {}

catch ( ClassNotFoundException e ) {}

}

return null;

}});

}

似乎对我不起作用(在Android 4.0+上).`com.android.internal.view.menu.IconMenuItemView`与任何东西都不匹配,当我把它更改为`com.android.internal.view.menu.ListMenuItemView`时,我的应用程序在打开菜单时崩溃了.使用`TextView`,我得到它来改变*菜单项的背景颜色的一部分,但不是一切.在每种情况下,这看起来都很糟糕,并且容易打破未来的Android版本. (3认同)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值