Android 修改Menu背景

public void setMenuBackground(Context context, Menu menu) {
		((Activity) context).getLayoutInflater().setFactory(new Factory() {
			@Override
			public View onCreateView(String name, Context context,
					AttributeSet attrs) {
				if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
					try {
						LayoutInflater f = ((Activity) context)
								.getLayoutInflater();
						final View view = f.createView(name, null, attrs);// 尝试创建我们自己布局
						new Handler().post(new Runnable() {
							public void run() {
								view.setBackgroundResource(R.drawable.menu_bg);// 设置背景为我们自定义的图片,替换cwj_bg文件即可
							}
						});
						return view;
					} catch (Exception e) {
					}
				}
				return null;
			}
		});
	}

你可能感兴趣的:(android)