rci*_*ati 19 android background menu layout-inflater
我正在尝试将标准浅灰色更改为浅绿色.似乎没有一种简单的方法可以做到这一点(例如通过Android主题),但我找到了一个解决方法,如本页所述:http://tinyurl.com/342dgn3.
作者似乎消失了,有人可以帮我整合这段代码吗?我不明白我需要在哪里实现LayoutInflater工厂类.
Abh*_*mar 11
当你给菜单充气时,调用这个setMenuBackground()方法
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu,menu);
setMenuBackground();
return true;
}
Run Code Online (Sandbox Code Playgroud)
并在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;
}});
}
Run Code Online (Sandbox Code Playgroud)
这显然是许多程序员所拥有的问题,Google尚未提供令人满意的支持解决方案.
Abhay Kumar和Nik Reiman发布的setMenuBackground()hack是一个很好的开始,但它在Android 2.3上崩溃或无效.
请在这个stackoverflow问题中查看我的答案(Louis Semprini),以获得更好的评论和更精致的黑客,它适用于2.1,2.2和2.3(并且这应该对3.X没有任何伤害,尽管我们永远无法保证这一点):
此外,以下是您可能会发现此问题的许多其他资源:
http://www.macadamian.com/blog/post/android_-_theming_the_unthemable/
http://www.codeproject.com/KB/android/AndroidMenusMyWay.aspx
| 归档时间: |
|
| 查看次数: |
88253 次 |
| 最近记录: |