如何设置ViewGroup的最大宽度?我使用的是Theme.Dialog
activity,然而,当调整到更大的屏幕时,它看起来不是很好,它也是一种轻量级的东西,我不希望它占据整个屏幕。
我尝试了this suggestion,但没有用。而且,没有像某些视图那样的android:maxWidth
属性。
有没有办法限制根LinearLayout,使其仅为(例如) 640个dip?为此,我愿意更改为另一个ViewGroup。
有什么建议吗?
发布于 2011-06-02 16:24:31
我所做的一种选择是扩展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>
编辑:这是我现在使用的实际代码。这仍然不是完整的,但在大多数情况下都是有效的。
发布于 2012-07-26 00:29:10
下面是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/
感谢你的问题和答案。你的回答对我帮助很大,我希望将来也能对其他人有所帮助。
发布于 2017-06-03 01:12:31
现在android.support.constraint.ConstraintLayout
让这一切变得更容易了。只需用ConstraintLayout包装您的视图(任何类型),并为视图设置以下属性:
android:layout_width="0dp"
app:layout_constraintWidth_default="spread"
app:layout_constraintWidth_max="640dp"
http://tools.android.com/recent/constraintlayoutbeta5isnowavailable
https://stackoverflow.com/questions/5875877
复制相似问题