在ViewGroup上设置最大宽度

如何设置ViewGroup的最大宽度? 我正在使用一个Theme.Dialog活动,但是,当调整到更大的屏幕时,这看起来不太好,它也是轻量级的,我不希望它占据整个屏幕。

我试过这个build议无济于事。 此外,没有像某些视图的android:maxWidth属性。

有没有办法限制根LinearLayout,以便它只是(例如)640浸? 我愿意为此更换为另一个ViewGroup。

有什么build议么?

我做的一个select是扩展LinearLayout并覆盖onMeasure函数。 例如:

 public class BoundedLinearLayout extends LinearLayout { private final int mBoundedWidth; private final int mBoundedHeight; public BoundedLinearLayout(Context context) { super(context); mBoundedWidth = 0; mBoundedHeight = 0; } public BoundedLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoundedView); mBoundedWidth = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_width, 0); mBoundedHeight = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_height, 0); a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Adjust width as necessary int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); if(mBoundedWidth > 0 && mBoundedWidth < measuredWidth) { int measureMode = MeasureSpec.getMode(widthMeasureSpec); widthMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedWidth, measureMode); } // Adjust height as necessary int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); if(mBoundedHeight > 0 && mBoundedHeight < measuredHeight) { int measureMode = MeasureSpec.getMode(heightMeasureSpec); heightMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedHeight, measureMode); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 

那么你的XML将使用自定义类:

 <com.yourpackage.BoundedLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" app:bounded_width="900dp"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </com.youpackage.BoundedLinearLayout> 

和attr.xml文件条目

 <declare-styleable name="BoundedView"> <attr name="bounded_width" format="dimension" /> <attr name="bounded_height" format="dimension" /> </declare-styleable> 

编辑:这是我现在使用的实际代码。 这仍然不完整,但在大多数情况下工作。

基于Chase(+1)的原始答案,我会做一些改变(如下所述)。

  1. 我会通过自定义属性设置最大宽度(代码下面的XML)

  2. 我会先调用super.measure() ,然后进行Math.min(*)比较。 使用原始答案代码时,如果在MeasureSpec设置的传入尺寸是LayoutParams.WRAP_CONTENTLayoutParams.FILL_PARENT我们可能会遇到问题。 由于这些有效的常量分别具有-2-1的值,原来的Math.min(*)变得没有用处,因为它将保持这些值超过最大值,并且说测得的WRAP_CONTENT大于我们的最大值,这个检查不会抓住它。 我想象的是OP只想到确切的暗淡(为什么它很好)

     public class MaxWidthLinearLayout extends LinearLayout { private int mMaxWidth = Integer.MAX_VALUE; public MaxWidthLinearLayout(Context context) { super(context); } public MaxWidthLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MaxWidthLinearLayout); mMaxWidth = a.getDimensionPixelSize(R.styleable.MaxWidthLinearLayout_maxWidth, Integer.MAX_VALUE); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //get measured height if(getMeasuredWidth() > mMaxWidth){ setMeasuredDimension(mMaxWidth, getMeasuredHeight()); } } } 

和XML的属性

  <!-- MaxWidthLinearLayout --> <declare-styleable name="MaxWidthLinearLayout"> <attr name="maxWidth" format="dimension" /> </declare-styleable> 

这是Dori答案的更好的代码。

在方法onMeasure ,如果调用super.onMeasure(widthMeasureSpec, heightMeasureSpec); 首先在该方法中,布局中的所有对象的宽度将不会改变。 因为他们在设置布局(父)宽度之前进行了初始化。

 public class MaxWidthLinearLayout extends LinearLayout { private final int mMaxWidth; public MaxWidthLinearLayout(Context context) { super(context); mMaxWidth = 0; } public MaxWidthLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MaxWidthLinearLayout); mMaxWidth = a.getDimensionPixelSize(R.styleable.MaxWidthLinearLayout_maxWidth, Integer.MAX_VALUE); a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); if (mMaxWidth > 0 && mMaxWidth < measuredWidth) { int measureMode = MeasureSpec.getMode(widthMeasureSpec); widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, measureMode); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } 

这里是一个使用xml attr的链接:
http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/

感谢这个问题和答案。 你的回答帮了我很大的忙,希望能帮助别人。

现在android.support.constraint.ConstraintLayout使得它更容易。 只要用ConstraintLayout包装你的视图(任何types),并将下列属性设置为视图:

 android:layout_width="0dp" app:layout_constraintWidth_default="spread" app:layout_constraintWidth_max="640dp" 

http://tools.android.com/recent/constraintlayoutbeta5isnowavailable

将外部布局或Viewgroup图层添加到当前布局文件。 此布局的高度和宽度将是最大高度/宽度。 现在你的内部布局可以设置为包装内容,并受外部布局的限制。 例如:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- OuterLayout to set Max Height and Width -- Add this ViewGroup to your layout File --> <LinearLayout android:id="@+id/outerLayout" android:layout_width="650dp" android:layout_height="650dp" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout> </LinearLayout> 

这是一个简单的答案,

宽度/高度似乎总是必须放在一起。 这在我看来是有效的。

  <Button android:text="Center" android:layout_width="100dp" android:layout_height="fill_parent" android:id="@+id/selectionCenterButton" android:minWidth="50dp" android:minHeight="50dp" android:maxWidth="100dp" android:maxHeight="50dp" android:layout_weight="1" /> 

该button的父级设置为包装内容,因此可以缩小,但最多可达400个宽度(4个button)。