| 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.content.Context; |
| 20 |
import android.content.res.TypedArray; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.util.DisplayMetrics; |
| 23 |
import android.widget.LinearLayout; |
| 24 |
|
| 25 |
import static android.view.View.MeasureSpec.*; |
| 26 |
import static com.android.internal.R.*; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class WeightedLinearLayout extends LinearLayout { |
| 33 |
private float mMajorWeightMin; |
| 34 |
private float mMinorWeightMin; |
| 35 |
private float mMajorWeightMax; |
| 36 |
private float mMinorWeightMax; |
| 37 |
|
| 38 |
public WeightedLinearLayout(Context context) { |
| 39 |
super(context); |
| 40 |
} |
| 41 |
|
| 42 |
public WeightedLinearLayout(Context context, AttributeSet attrs) { |
| 43 |
super(context, attrs); |
| 44 |
|
| 45 |
TypedArray a = |
| 46 |
context.obtainStyledAttributes(attrs, styleable.WeightedLinearLayout); |
| 47 |
|
| 48 |
mMajorWeightMin = a.getFloat(styleable.WeightedLinearLayout_majorWeightMin, 0.0f); |
| 49 |
mMinorWeightMin = a.getFloat(styleable.WeightedLinearLayout_minorWeightMin, 0.0f); |
| 50 |
mMajorWeightMax = a.getFloat(styleable.WeightedLinearLayout_majorWeightMax, 0.0f); |
| 51 |
mMinorWeightMax = a.getFloat(styleable.WeightedLinearLayout_minorWeightMax, 0.0f); |
| 52 |
|
| 53 |
a.recycle(); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 58 |
final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); |
| 59 |
final int screenWidth = metrics.widthPixels; |
| 60 |
final boolean isPortrait = screenWidth < metrics.heightPixels; |
| 61 |
|
| 62 |
final int widthMode = getMode(widthMeasureSpec); |
| 63 |
|
| 64 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 65 |
|
| 66 |
int width = getMeasuredWidth(); |
| 67 |
boolean measure = false; |
| 68 |
|
| 69 |
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY); |
| 70 |
|
| 71 |
final float widthWeightMin = isPortrait ? mMinorWeightMin : mMajorWeightMin; |
| 72 |
final float widthWeightMax = isPortrait ? mMinorWeightMax : mMajorWeightMax; |
| 73 |
if (widthMode == AT_MOST) { |
| 74 |
final int weightedMin = (int) (screenWidth * widthWeightMin); |
| 75 |
final int weightedMax = (int) (screenWidth * widthWeightMin); |
| 76 |
if (widthWeightMin > 0.0f && width < weightedMin) { |
| 77 |
widthMeasureSpec = MeasureSpec.makeMeasureSpec(weightedMin, EXACTLY); |
| 78 |
measure = true; |
| 79 |
} else if (widthWeightMax > 0.0f && width > weightedMax) { |
| 80 |
widthMeasureSpec = MeasureSpec.makeMeasureSpec(weightedMax, EXACTLY); |
| 81 |
measure = true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (measure) { |
| 88 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 89 |
} |
| 90 |
} |
| 91 |
} |