BaselineLayout.java revision 583f64754e19245bf81e8a23e87b4c03dc475786
1/* 2 * Copyright (C) 2016 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 17package android.support.design.internal; 18 19import android.content.Context; 20import android.support.v4.view.ViewCompat; 21import android.support.v7.widget.ViewUtils; 22import android.util.AttributeSet; 23import android.view.View; 24import android.view.ViewGroup; 25 26/** 27 * A simple ViewGroup that aligns all the views inside on a baseline. Note: bottom padding for this 28 * view will be measured starting from the baseline. 29 * 30 * @hide 31 */ 32public class BaselineLayout extends ViewGroup { 33 private int mBaseline = -1; 34 35 public BaselineLayout(Context context) { 36 super(context, null, 0); 37 } 38 39 public BaselineLayout(Context context, AttributeSet attrs) { 40 super(context, attrs, 0); 41 } 42 43 public BaselineLayout(Context context, AttributeSet attrs, int defStyleAttr) { 44 super(context, attrs, defStyleAttr); 45 } 46 47 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 48 final int count = getChildCount(); 49 int maxWidth = 0; 50 int maxHeight = 0; 51 int maxChildBaseline = -1; 52 int maxChildDescent = -1; 53 int childState = 0; 54 55 for (int i = 0; i < count; i++) { 56 final View child = getChildAt(i); 57 if (child.getVisibility() == GONE) { 58 continue; 59 } 60 61 measureChild(child, widthMeasureSpec, heightMeasureSpec); 62 final int baseline = child.getBaseline(); 63 if (baseline != -1) { 64 maxChildBaseline = Math.max(maxChildBaseline, baseline); 65 maxChildDescent = Math.max(maxChildDescent, child.getMeasuredHeight() - baseline); 66 } 67 maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); 68 maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); 69 childState = ViewUtils.combineMeasuredStates(childState, 70 ViewCompat.getMeasuredState(child)); 71 } 72 if (maxChildBaseline != -1) { 73 maxChildDescent = Math.max(maxChildDescent, getPaddingBottom()); 74 maxHeight = Math.max(maxHeight, maxChildBaseline + maxChildDescent); 75 mBaseline = maxChildBaseline; 76 } 77 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); 78 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); 79 setMeasuredDimension( 80 ViewCompat.resolveSizeAndState(maxWidth, widthMeasureSpec, childState), 81 ViewCompat.resolveSizeAndState(maxHeight, heightMeasureSpec, 82 childState << MEASURED_HEIGHT_STATE_SHIFT)); 83 } 84 85 @Override 86 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 87 final int count = getChildCount(); 88 final int parentLeft = getPaddingLeft(); 89 final int parentRight = right - left - getPaddingRight(); 90 final int parentContentWidth = parentRight - parentLeft; 91 final int parentTop = getPaddingTop(); 92 93 for (int i = 0; i < count; i++) { 94 final View child = getChildAt(i); 95 if (child.getVisibility() == GONE) { 96 continue; 97 } 98 99 final int width = child.getMeasuredWidth(); 100 final int height = child.getMeasuredHeight(); 101 102 final int childLeft = parentLeft + (parentContentWidth - width) / 2; 103 final int childTop; 104 if (mBaseline != -1 && child.getBaseline() != -1) { 105 childTop = parentTop + mBaseline - child.getBaseline(); 106 } else { 107 childTop = parentTop; 108 } 109 110 child.layout(childLeft, childTop, childLeft + width, childTop + height); 111 } 112 } 113 114 @Override 115 public int getBaseline() { 116 return mBaseline; 117 } 118} 119