OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.widget; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.res.TypedArray; |
| 9 import android.util.AttributeSet; |
| 10 import android.widget.LinearLayout; |
| 11 |
| 12 import com.google.android.apps.chrome.R; |
| 13 |
| 14 /** |
| 15 * A LinearLayout that can be constrained to a maximum width. |
| 16 * |
| 17 * Example: |
| 18 * <org.chromium.chrome.browser.widget.BoundedLinearLayout |
| 19 * xmlns:android="http://schemas.android.com/apk/res/android" |
| 20 * xmlns:chrome="http://schemas.android.com/apk/res-auto" |
| 21 * android:layout_width="match_parent" |
| 22 * android:layout_height="match_parent" |
| 23 * chrome:maxWidth="692dp" > |
| 24 * ... |
| 25 */ |
| 26 public class BoundedLinearLayout extends LinearLayout { |
| 27 |
| 28 private static final int NO_MAX_WIDTH = -1; |
| 29 |
| 30 private final int mMaxWidth; |
| 31 |
| 32 /** |
| 33 * Constructor for inflating from XML. |
| 34 */ |
| 35 public BoundedLinearLayout(Context context, AttributeSet attrs) { |
| 36 super(context, attrs); |
| 37 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Bounded
View); |
| 38 mMaxWidth = a.getDimensionPixelSize(R.styleable.BoundedView_maxWidth, NO
_MAX_WIDTH); |
| 39 a.recycle(); |
| 40 } |
| 41 |
| 42 @Override |
| 43 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 44 // Limit width to mMaxWidth. |
| 45 int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 46 if (mMaxWidth != NO_MAX_WIDTH && widthSize > mMaxWidth) { |
| 47 int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 48 if (widthMode == MeasureSpec.UNSPECIFIED) widthMode = MeasureSpec.AT
_MOST; |
| 49 widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, widthMode)
; |
| 50 } |
| 51 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 52 } |
| 53 } |
OLD | NEW |