| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.view.View; |
| 23 |
import android.widget.LinearLayout; |
| 24 |
import android.widget.RemoteViews; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
@RemoteViews.RemoteView |
| 33 |
public class RemeasuringLinearLayout extends LinearLayout { |
| 34 |
|
| 35 |
private ArrayList<View> mMatchParentViews = new ArrayList<>(); |
| 36 |
|
| 37 |
public RemeasuringLinearLayout(Context context) { |
| 38 |
super(context); |
| 39 |
} |
| 40 |
|
| 41 |
public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs) { |
| 42 |
super(context, attrs); |
| 43 |
} |
| 44 |
|
| 45 |
public RemeasuringLinearLayout(Context context, @Nullable AttributeSet attrs, |
| 46 |
int defStyleAttr) { |
| 47 |
super(context, attrs, defStyleAttr); |
| 48 |
} |
| 49 |
|
| 50 |
public RemeasuringLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, |
| 51 |
int defStyleRes) { |
| 52 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 57 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 58 |
int count = getChildCount(); |
| 59 |
int height = 0; |
| 60 |
boolean isVertical = getOrientation() == LinearLayout.VERTICAL; |
| 61 |
boolean isWrapContent = getLayoutParams().height == LayoutParams.WRAP_CONTENT; |
| 62 |
for (int i = 0; i < count; ++i) { |
| 63 |
final View child = getChildAt(i); |
| 64 |
if (child == null || child.getVisibility() == View.GONE) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 69 |
if (!isWrapContent || lp.height != LayoutParams.MATCH_PARENT || isVertical) { |
| 70 |
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; |
| 71 |
height = Math.max(height, isVertical ? height + childHeight : childHeight); |
| 72 |
} else { |
| 73 |
|
| 74 |
|
| 75 |
mMatchParentViews.add(child); |
| 76 |
} |
| 77 |
} |
| 78 |
if (mMatchParentViews.size() > 0) { |
| 79 |
int exactHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
| 80 |
for (View child : mMatchParentViews) { |
| 81 |
child.measure(getChildMeasureSpec( |
| 82 |
widthMeasureSpec, getPaddingStart() + getPaddingEnd(), |
| 83 |
child.getLayoutParams().width), |
| 84 |
exactHeightSpec); |
| 85 |
} |
| 86 |
} |
| 87 |
mMatchParentViews.clear(); |
| 88 |
setMeasuredDimension(getMeasuredWidth(), height); |
| 89 |
} |
| 90 |
} |