当前位置:网站首页>Summary of knowledge points of customized ViewGroup - continuously updated

Summary of knowledge points of customized ViewGroup - continuously updated

2022-06-11 07:56:00 tinyvampirepudge

Customize ViewGroup Summary of knowledge points of - Continuous updating

1、child.getMeasuredWidth() Will contain child Of padding value

child Of margin The value of needs to adapt itself .

2、 stay onMeasure In the method :

It needs to be right first child Conduct measure, Then you can get measuredWidth and measureHeight.

There are two commonly used measurement methods :
ViewGroup#measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)

ViewGroup#measureChildWithMargins(View child,int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

however measureChildWithMargins There's a flaw , Its internal MarginLayoutParams It's a strong turn of type , Non empty judgment and type judgment are not added , If we are custom ViewGroup, This may lead to null pointer and type conversion exceptions , To solve this problem , I customized a method , Please see the following for details measureChildWithMarginsAndUsedSpace Method .

3、 measurement child when , in consideration of measureChildWithMargins The defects of , Here we implement a method by ourselves :measureChildWithMarginsAndUsedSpace

protected void measureChildWithMarginsAndUsedSpace(View child,
                                                   int parentWidthMeasureSpec, int widthUsed,
                                                   int parentHeightMeasureSpec, int heightUsed) {
    //  The bottom line , Not going to happen 
    if (child.getLayoutParams() == null) {
        return;
    }
    final LayoutParams lp = child.getLayoutParams();


    int paddingH = getPaddingLeft() + getPaddingRight() + widthUsed;
    int paddingV = getPaddingTop() + getPaddingBottom() + heightUsed;


    if (child.getLayoutParams() instanceof MarginLayoutParams) {
        final MarginLayoutParams mlp = (MarginLayoutParams) child.getLayoutParams();
        paddingH += mlp.leftMargin + mlp.rightMargin;
        paddingV += mlp.topMargin + mlp.bottomMargin;
    }


    final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, paddingH, lp.width);
    final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, paddingV, lp.height);


    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

4、 Customize ViewGroup when , You must consider your own padding, as well as child Of padding、margin etc. .

If child It's also custom ViewGroup, Then there are also factors to consider .

5、 How to easily support child Of margin data ? rewrite ViewGroup#generateLayoutParams The method can

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new MarginLayoutParams(getContext(), attrs);
}

ViewGroup Provided only ViewGroup.LayoutParams,MarginLayoutParams It's the individual ViewGroup Self inherited .

our ViewGroup To support MarginLayoutParams The best way , Is to rewrite ViewGroup#generateLayoutParams Method .

6、 For some operations that need to limit the width , Can be in onMeasure In the operation .

Measure the actual width first , If you exceed your expectations , And then carry out secondary measurement according to the maximum length .
common FlowLayout Etc , Can be achieved in this way .

To be continued , Coming soon .

原网站

版权声明
本文为[tinyvampirepudge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110747210571.html