DrawerLayout must be measured with MeasureSpec.EXACTLY error

前端 未结 12 2390
独厮守ぢ
独厮守ぢ 2020-12-03 16:54

I am trying to implement a Navigation drawer, but I keep getting this error. I saw the similar questions but did not work for me. I have the following layout activity_main2.

相关标签:
12条回答
  • 2020-12-03 17:27

    You could try to set Exactly measurements explicitly always like below:

    public class MyDrawerLayout extends DrawerLayout {
    
        public MyDrawerLayout(@NonNull Context context) {
            super(context);
        }
    
        public MyDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
    
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:31

    Even I tried the Drawer's layout_height = "match_parent" but also java threw an error, so I just restarted android studio it worked no errors were found

    0 讨论(0)
  • 2020-12-03 17:33

    This can also happen if you have set layout_width to wrap_content. In my case it was solved by setting width to match_parent. Hope this helps someone.

    0 讨论(0)
  • 2020-12-03 17:39

    Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.

    0 讨论(0)
  • 2020-12-03 17:39

    By mistake, I set a custom dialog theme to the activity instead of a custom activity theme:

    activity.setTheme(R.style.StyledDialogTheme);
    

    After i corrected this, it workes again.

    0 讨论(0)
  • 2020-12-03 17:40

    For those who still have an exception, ensure your DrawerLayout's height is match_parent

    0 讨论(0)
提交回复
热议问题