Inflated children of custom LinearLayout don't show when overriding onMeasure

前端 未结 1 1638
借酒劲吻你
借酒劲吻你 2021-01-21 14:01

I\'m trying to show a MyCustomLinearLayout which extends LinearLayout. I\'m inflating the MyCustomLinearLayout with the android:layo

1条回答
  •  一整个雨季
    2021-01-21 14:40

    That's not how you override the onMeasure method especially of a default SDK layout. Right now with your code you just made the MyCustomLinearLayout square assigning it a certain value. But, you didn't measured its children so they are sizeless and don't appear on the screen.

    I'm not sure this would work but try this:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = getMeasuredHeight();
        super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY));
    }
    

    Of course this will basically do the work of onMeasure twice but the ImageView should be now visible filling it's parent. There are other solutions but your question is a bit scarce on details.

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