// 关键代码为重写Layout.Factory.onCreateView()方法自定义布局
protected void setMenuBackground() {
MenuAct.this.getLayoutInflater().setFactory(new android.view.LayoutInflater.Factory() {
/**
* name - Tag name to be inflated.<br/>
* context - The context the view is being created in.<br/>
* attrs - Inflation attributes as specified in XML file.<br/>
*/
public View onCreateView(String name, Context context, AttributeSet attrs) {
// 指定自定义inflate的对象
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// 设置背景图片
view.setBackgroundResource(R.drawable.menu_background);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
});
}
protected void setMenuBackground() {
MenuAct.this.getLayoutInflater().setFactory(new android.view.LayoutInflater.Factory() {
/**
* name - Tag name to be inflated.<br/>
* context - The context the view is being created in.<br/>
* attrs - Inflation attributes as specified in XML file.<br/>
*/
public View onCreateView(String name, Context context, AttributeSet attrs) {
// 指定自定义inflate的对象
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// 设置背景图片
view.setBackgroundResource(R.drawable.menu_background);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
});
}