FileDocCategorySizeDatePackage
WeightedLinearLayout.javaAPI DocAndroid 5.1 API3399Thu Mar 12 22:22:10 GMT 2015com.android.internal.widget

WeightedLinearLayout

public class WeightedLinearLayout extends android.widget.LinearLayout
A special layout when measured in AT_MOST will take up a given percentage of the available space.

Fields Summary
private float
mMajorWeightMin
private float
mMinorWeightMin
private float
mMajorWeightMax
private float
mMinorWeightMax
Constructors Summary
public WeightedLinearLayout(android.content.Context context)

        super(context);
    
public WeightedLinearLayout(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
        
        TypedArray a = 
            context.obtainStyledAttributes(attrs, styleable.WeightedLinearLayout);

        mMajorWeightMin = a.getFloat(styleable.WeightedLinearLayout_majorWeightMin, 0.0f);
        mMinorWeightMin = a.getFloat(styleable.WeightedLinearLayout_minorWeightMin, 0.0f);
        mMajorWeightMax = a.getFloat(styleable.WeightedLinearLayout_majorWeightMax, 0.0f);
        mMinorWeightMax = a.getFloat(styleable.WeightedLinearLayout_minorWeightMax, 0.0f);
        
        a.recycle();
    
Methods Summary
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        final int screenWidth = metrics.widthPixels;
        final boolean isPortrait = screenWidth < metrics.heightPixels;

        final int widthMode = getMode(widthMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        boolean measure = false;

        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);

        final float widthWeightMin = isPortrait ? mMinorWeightMin : mMajorWeightMin;
        final float widthWeightMax = isPortrait ? mMinorWeightMax : mMajorWeightMax;
        if (widthMode == AT_MOST) {
            final int weightedMin = (int) (screenWidth * widthWeightMin);
            final int weightedMax = (int) (screenWidth * widthWeightMin);
            if (widthWeightMin > 0.0f && width < weightedMin) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(weightedMin, EXACTLY);
                measure = true;
            } else if (widthWeightMax > 0.0f && width > weightedMax) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(weightedMax, EXACTLY);
                measure = true;
            }
        }

        // TODO: Support height?

        if (measure) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }