Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2014 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 android.theme.app;
     18 
     19 import android.theme.app.R;
     20 
     21 import android.content.Context;
     22 import android.content.res.Resources;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 
     27 /**
     28  * {@link ViewGroup} that inflates to a reference width and height.
     29  */
     30 public class ReferenceViewGroup extends ViewGroup {
     31 
     32     private final int mWidthDp;
     33     private final int mHeightDp;
     34 
     35     public ReferenceViewGroup(Context context) {
     36         this(context, null);
     37     }
     38 
     39     public ReferenceViewGroup(Context context, AttributeSet attrs) {
     40         super(context, attrs);
     41         Resources resources = context.getResources();
     42         mWidthDp = resources.getDimensionPixelSize(R.dimen.reference_width);
     43         mHeightDp = resources.getDimensionPixelSize(R.dimen.reference_height);
     44     }
     45 
     46     @Override
     47     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     48         widthMeasureSpec = getMeasureSpec(LayoutParams.MATCH_PARENT, mWidthDp);
     49         heightMeasureSpec = getMeasureSpec(LayoutParams.MATCH_PARENT, mHeightDp);
     50 
     51         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     52 
     53         int childCount = getChildCount();
     54         for (int i = 0; i < childCount; i++) {
     55             View child = getChildAt(i);
     56             LayoutParams params = child.getLayoutParams();
     57             int width = getMeasureSpec(params.width, mWidthDp);
     58             int height = getMeasureSpec(params.height, mHeightDp);
     59             child.measure(width, height);
     60         }
     61     }
     62 
     63     private int getMeasureSpec(int value, int size) {
     64         if (value == LayoutParams.MATCH_PARENT) {
     65             return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
     66         } else if (value == LayoutParams.WRAP_CONTENT) {
     67             return MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST);
     68         } else {
     69             return value;
     70         }
     71     }
     72 
     73     @Override
     74     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     75         int childCount = getChildCount();
     76         for (int i = 0; i < childCount; i++) {
     77             View child = getChildAt(i);
     78             child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
     79         }
     80     }
     81 }
     82