Correctly layout children with onLayout in Custom ViewGroup

末鹿安然 提交于 2021-01-29 03:17:47

问题


I am creating a custom ViewGroup. I does need to have 2 FrameLayout one above the other; the one that stays to the bottom must be 20dp, while the other one must cover the rest of the view.

onMeasure

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(widthSize, heightSize);

    final View content = getChildAt(CONTENT_INDEX);
    final View bar = getChildAt(BAR_INDEX);

    content.measure(
        MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
        MeasureSpec.makeMeasureSpec(heightSize - getPixels(BAR_HEIGHT), MeasureSpec.EXACTLY)
    );

    bar.measure(
        MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
        MeasureSpec.makeMeasureSpec(getPixels(BAR_HEIGHT), MeasureSpec.EXACTLY)
    );

onLayout

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mInLayout = true;

    final View content = getChildAt(CONTENT_INDEX);
    final View bar = getChildAt(BAR_INDEX);

    if (content.getVisibility() != GONE) {
        content.layout(0, 0, content.getMeasuredWidth(), content.getMeasuredHeight());
     }

     if (bar.getVisibility() != GONE) {
         bar.layout(0, content.getMeasuredHeight(), bar.getMeasuredWidth(), 0);
     }

     mInLayout = false;
     mFirstLayout = false;
 }
    }

The views I am adding to this custom ViewGroup

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

mContentContainer = new FrameLayout(getContext());
mContentContainer.setLayoutParams(lp);

mBarContainer = new FrameLayout(getContext());
mBarContainer.setLayoutParams(lp);

// ... adding stuff to both containers ....

addView(mContentContainer, 0);
addView(mBarContainer, 1);

The issue

mContentContainer gets rendered correctly (from top=0 to bottom=(totalHeight - bar height) and match parent for the width), while the bar is not rendered.


回答1:


The last parameter in the View#layout() method is the bottom of the View. For your bar, you're passing 0, but it should be the height of your custom View, which you can figure from the t and b values passed into onLayout().

bar.layout(0, content.getMeasuredHeight(), bar.getMeasuredWidth(), b - t);


来源:https://stackoverflow.com/questions/38810279/correctly-layout-children-with-onlayout-in-custom-viewgroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!