android optionmenu 怎么定义背景颜色色

2025-04-15 10:00:37
推荐回答(1个)
回答1:

protected void setMenuBackGround() {
  getLayoutInflater().setFactory(
    new android.view.LayoutInflater.Factory() {
 
     @Override
     public View onCreateView(String name, Context context,
       AttributeSet attrs) {
      // TODO Auto-generated method stub
      // 指定自定义inflater的对象
      if (name
        .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
       try {
        LayoutInflater inflater = getLayoutInflater();
        final View view = inflater.createView(name,
          null, attrs);
        new Handler().post(new Runnable() {
         public void run() {
          view
            .setBackgroundResource(R.drawable.application_background_static);
         }
        });
        return view;
       } catch (InflateException e) {
        e.printStackTrace();
       } catch (ClassNotFoundException e) {
        e.printStackTrace();
       }
      }
      return null;
     }
    });
 }

 如果2.3以上修改没效果,把 getLayoutInflater()改为 (LayoutInflater) context  .getSystemService(Context.LAYOUT_INFLATER_SERVICE)