In Robolectric, how do I get around DrawerLayout must be measured with MeasureSpec.EXACTLY error?

前端 未结 3 1343
既然无缘
既然无缘 2020-12-30 02:46

I\'m trying to add a DrawerLayout to my application for navigation, and I\'m testing my application with Robolectric (I\'ve seen this problem both with Robolectric 2.1.1 and

相关标签:
3条回答
  • 2020-12-30 03:23

    After some digging, I found a solution. It seems that Robolectric is somehow misscalculating the MeasureSpecs, which leads to this issue. In my case I could fix this problem by extending the DrawerLayout, and overriding onMeasure() :

    public class CustomDrawerLayout extends DrawerLayout {
    
        public CustomDrawerLayout(Context context) {
            super(context);
        }
    
        public CustomDrawerLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomDrawerLayout(Context context, 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-30 03:30

    Just run your program on your android device. It runs perfectly. Just some problem in Eclipse i guess.

    0 讨论(0)
  • 2020-12-30 03:46

    Update your Support Library jar: http://developer.android.com/tools/support-library/setup.html

    And make sure that your project refers the latest one: How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA

    Example path to the lib: [adt-bundle-windows]\sdk\extras\android\support\v4\android-support-v4.jar

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