BaselineLayout.java revision 560426d9e9d9a9f29ed1a4abb0ee9cf837ed8a4f
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.
28 *
29 * @hide
30 */
31public class BaselineLayout extends ViewGroup {
32    private int mBaseline = -1;
33
34    public BaselineLayout(Context context) {
35        super(context, null, 0);
36    }
37
38    public BaselineLayout(Context context, AttributeSet attrs) {
39        super(context, attrs, 0);
40    }
41
42    public BaselineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
43        super(context, attrs, defStyleAttr);
44    }
45
46    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
47        final int count = getChildCount();
48        int maxWidth = 0;
49        int maxHeight = 0;
50        int maxChildBaseline = -1;
51        int maxChildDescent = -1;
52        int childState = 0;
53
54        for (int i = 0; i < count; i++) {
55            final View child = getChildAt(i);
56            if (child.getVisibility() == GONE) {
57                continue;
58            }
59
60            measureChild(child, widthMeasureSpec, heightMeasureSpec);
61            final int baseline = child.getBaseline();
62            if (baseline != -1) {
63                maxChildBaseline = Math.max(maxChildBaseline, baseline);
64                maxChildDescent = Math.max(maxChildDescent, child.getMeasuredHeight() - baseline);
65            }
66            maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
67            maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
68            childState = ViewUtils.combineMeasuredStates(childState,
69                    ViewCompat.getMeasuredState(child));
70        }
71        if (maxChildBaseline != -1) {
72            maxHeight = Math.max(maxHeight, maxChildBaseline + maxChildDescent);
73            mBaseline = maxChildBaseline;
74        }
75        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
76        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
77        setMeasuredDimension(
78                ViewCompat.resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
79                ViewCompat.resolveSizeAndState(maxHeight, heightMeasureSpec,
80                        childState << MEASURED_HEIGHT_STATE_SHIFT));
81    }
82
83    @Override
84    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85        final int count = getChildCount();
86        final int parentLeft = getPaddingLeft();
87        final int parentRight = right - left - getPaddingRight();
88        final int parentContentWidth = parentRight - parentLeft;
89        final int parentTop = getPaddingTop();
90
91        for (int i = 0; i < count; i++) {
92            final View child = getChildAt(i);
93            if (child.getVisibility() == GONE) {
94                continue;
95            }
96
97            final int width = child.getMeasuredWidth();
98            final int height = child.getMeasuredHeight();
99
100            final int childLeft = parentLeft + (parentContentWidth - width) / 2;
101            final int childTop;
102            if (mBaseline != -1 && child.getBaseline() != -1) {
103                childTop = parentTop + mBaseline - child.getBaseline();
104            } else {
105                childTop = parentTop;
106            }
107
108            child.layout(childLeft, childTop, childLeft + width, childTop + height);
109        }
110    }
111
112    @Override
113    public int getBaseline() {
114        return mBaseline;
115    }
116}
117