wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.mm.android.deviceaddmodule.mobilecommon.widget;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
 
public class AverageLinearLayout extends ViewGroup{
 
    public AverageLinearLayout(Context context) {
        super(context);
    }
 
    public AverageLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public AverageLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getMeasuredWidth();
        int hight = getMeasuredHeight();
        int childCount = getChildCount();
        int spaceCount = childCount - 1;
        int childTotalWidth = 0;
        for(int i = 0; i < childCount; i++){
            final View child = getChildAt(i);
            if(child.getVisibility() == View.VISIBLE){
                final int childWidth = child.getMeasuredWidth();
                childTotalWidth += childWidth;
            }else{
                spaceCount--;
            }
 
        }
 
        int space = (width - getPaddingLeft() - getPaddingRight() - childTotalWidth) / spaceCount;
        int childLeft = getPaddingLeft();
 
        for(int i = 0; i < childCount; i++){
            final View child = getChildAt(i);
            if(child.getVisibility() == View.VISIBLE){
                final int childWidth = child.getMeasuredWidth();
                final int childHeight = child.getMeasuredHeight();
 
                int childTop =(hight - getPaddingTop() - getPaddingBottom() - childHeight) / 2;
 
                if(i != childCount -1){
                    child.layout(childLeft ,childTop , childLeft  + childWidth, childTop + childHeight);
 
                    childLeft += space;
 
                }else{
                    child.layout(r - getPaddingRight() - childWidth ,childTop , r - getPaddingRight(), childTop + childHeight);
                }
 
                childLeft += childWidth;
            }
        }
 
 
    }
 
 
 
}