Activity里添加ViewGroup ViewGroup里添加ViewGroup

举例:Activity里面添加ViewGroup

1. browserActivity  extends Activity

2. desktopLayout  extends ViewGroup

第一种方法:

 browserActivity原本的布局:setContentView(R.layout.activity_main);

当点击某按钮后,添加一个新的布局,如下所示,布局变为mDesktopManagerLayout

browserActivity.setContentView(mDesktopManagerLayout);

第二种方法:

在browserActivity中使用WindowManager

在browserActivity上面attach一个新的viewgroup

 ViewManager mWindowManager  =  ( (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

 mWindowManager.addView(mDesktopManagerLayout, this.getWindowParams());

//mDesktopManagerLayout相对于browserActivity的布局如下:

    private WindowManager.LayoutParams getWindowParams() {
        if (mLayoutParams == null) {
            mLayoutParams = new WindowManager.LayoutParams();
        }
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
        mLayoutParams.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                            |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                            |WindowManager.LayoutParams.FLAG_FULLSCREEN;
        mLayoutParams.format = PixelFormat.TRANSLUCENT;
        mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
       mLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        mLayoutParams.token = null;
        return mLayoutParams;
    }

举例:ViewGroup里面添加ViewGroup

  mDesktopManagerLayout = new DesktopManagerLayout(mContext);
  MiuiDeskTopManagerLayout.addView(mDesktopManagerLayout);


你可能感兴趣的:(android)