Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     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  * A special layout when measured in AT_MOST will take up a given percentage of
     30  * the available space.
     31  */
     32 public class WeightedLinearLayout extends LinearLayout {
     33     private float mMajorWeight;
     34     private float mMinorWeight;
     35 
     36     public WeightedLinearLayout(Context context) {
     37         super(context);
     38     }
     39 
     40     public WeightedLinearLayout(Context context, AttributeSet attrs) {
     41         super(context, attrs);
     42 
     43         TypedArray a =
     44             context.obtainStyledAttributes(attrs, styleable.WeightedLinearLayout);
     45 
     46         mMajorWeight = a.getFloat(styleable.WeightedLinearLayout_majorWeight, 0.0f);
     47         mMinorWeight = a.getFloat(styleable.WeightedLinearLayout_minorWeight, 0.0f);
     48 
     49         a.recycle();
     50     }
     51 
     52     @Override
     53     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     54         final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
     55         final int screenWidth = metrics.widthPixels;
     56         final boolean isPortrait = screenWidth < metrics.heightPixels;
     57 
     58         final int widthMode = getMode(widthMeasureSpec);
     59 
     60         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     61 
     62         int width = getMeasuredWidth();
     63         int height = getMeasuredHeight();
     64         boolean measure = false;
     65 
     66         widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);
     67         heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, EXACTLY);
     68 
     69         final float widthWeight = isPortrait ? mMinorWeight : mMajorWeight;
     70         if (widthMode == AT_MOST && widthWeight > 0.0f) {
     71             if (width < (screenWidth * widthWeight)) {
     72                 widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (screenWidth * widthWeight),
     73                         EXACTLY);
     74                 measure = true;
     75             }
     76         }
     77 
     78         // TODO: Support height?
     79 
     80         if (measure) {
     81             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     82         }
     83     }
     84 }
     85