OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.TypedArray; | 8 import android.content.res.TypedArray; |
9 import android.util.AttributeSet; | 9 import android.util.AttributeSet; |
10 import android.widget.LinearLayout; | 10 import android.widget.LinearLayout; |
11 | 11 |
12 import org.chromium.chrome.R; | 12 import org.chromium.chrome.R; |
13 | 13 |
14 /** | 14 /** |
15 * A LinearLayout that can be constrained to a maximum width. | 15 * A LinearLayout that can be constrained to a maximum size. |
16 * | 16 * |
17 * Example: | 17 * Example: |
18 * <org.chromium.chrome.browser.widget.BoundedLinearLayout | 18 * <org.chromium.chrome.browser.widget.BoundedLinearLayout |
19 * xmlns:android="http://schemas.android.com/apk/res/android" | 19 * xmlns:android="http://schemas.android.com/apk/res/android" |
20 * xmlns:chrome="http://schemas.android.com/apk/res-auto" | 20 * xmlns:chrome="http://schemas.android.com/apk/res-auto" |
21 * android:layout_width="match_parent" | 21 * android:layout_width="match_parent" |
22 * android:layout_height="match_parent" | 22 * android:layout_height="match_parent" |
23 * chrome:maxWidth="692dp" > | 23 * chrome:maxWidth="692dp" > |
24 * ... | 24 * ... |
25 */ | 25 */ |
26 public class BoundedLinearLayout extends LinearLayout { | 26 public class BoundedLinearLayout extends LinearLayout { |
27 | 27 |
28 private static final int NO_MAX_WIDTH = -1; | 28 private static final int NOT_SPECIFIED = -1; |
29 | 29 |
30 private final int mMaxWidth; | 30 private final int mMaxWidth; |
| 31 private final int mMaxHeight; |
31 | 32 |
32 /** | 33 /** |
33 * Constructor for inflating from XML. | 34 * Constructor for inflating from XML. |
34 */ | 35 */ |
35 public BoundedLinearLayout(Context context, AttributeSet attrs) { | 36 public BoundedLinearLayout(Context context, AttributeSet attrs) { |
36 super(context, attrs); | 37 super(context, attrs); |
37 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Bounded
View); | 38 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Bounded
View); |
38 mMaxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_maxWidth, NO
_MAX_WIDTH); | 39 mMaxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_maxWidth, NO
T_SPECIFIED); |
| 40 mMaxHeight = a.getDimensionPixelSize(R.styleable.BoundedView_maxHeight,
NOT_SPECIFIED); |
39 a.recycle(); | 41 a.recycle(); |
40 } | 42 } |
41 | 43 |
42 @Override | 44 @Override |
43 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | 45 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
44 // Limit width to mMaxWidth. | 46 // Limit width to mMaxWidth. |
45 int widthSize = MeasureSpec.getSize(widthMeasureSpec); | 47 int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
46 if (mMaxWidth != NO_MAX_WIDTH && widthSize > mMaxWidth) { | 48 if (mMaxWidth != NOT_SPECIFIED && widthSize > mMaxWidth) { |
47 int widthMode = MeasureSpec.getMode(widthMeasureSpec); | 49 int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
48 if (widthMode == MeasureSpec.UNSPECIFIED) widthMode = MeasureSpec.AT
_MOST; | 50 if (widthMode == MeasureSpec.UNSPECIFIED) widthMode = MeasureSpec.AT
_MOST; |
49 widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode)
; | 51 widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode)
; |
50 } | 52 } |
| 53 int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 54 if (mMaxHeight != NOT_SPECIFIED && heightSize > mMaxHeight) { |
| 55 int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 56 if (heightMode == MeasureSpec.UNSPECIFIED) heightMode = MeasureSpec.
AT_MOST; |
| 57 heightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, heightMo
de); |
| 58 } |
51 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | 59 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
52 } | 60 } |
53 } | 61 } |
OLD | NEW |