How to Display Navigation Drawer in all activities?

后端 未结 8 1234
刺人心
刺人心 2020-11-27 13:39

I have a Navigation Drawer which should appear in all my activities.

I saw many questions similar to this & found a solution like Extending the Main

8条回答
  •  粉色の甜心
    2020-11-27 14:14

    Ok here is hacky way to do this, I use it only for special kind of debug build to set properties of views in realtime (design tool).

    It has advantage that you can use your child activities as usual without, special behavior that is required in different answers.

    so in BaseActvity you can add:

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    
    // WARNING: Hacky, use carefully!!!
    ViewGroup androidBaseView = (ViewGroup) findViewById(android.R.id.content);
    //this one in what child activity has just set in setContentView()
    ViewGroup childContent = (ViewGroup) androidBaseView.getChildAt(0);
    
    View drawerView = LayoutInflater.from(this)
        .inflate(R.layout.base_activity_drawer, androidBaseView, false);
    FrameLayout frameLayout = (FrameLayout) drawerView.findViewById(R.id.content);
    androidBaseView.removeView(childContent);
    frameLayout.addView(childContent);
    androidBaseView.addView(drawerView);
    }
    

    and xml for drawer is just:

      
      
      
    
    

提交回复
热议问题