如何设置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/
感谢你的问题和答案。你的回答对我帮助很大,我希望将来也能对其他人有所帮助。
发布于 2011-12-24 02:30:21
在Chase (+1)的原始答案的基础上,我将做一些更改(概述如下)。
super.measure(),然后进行Math.min(*)比较。使用原始答案代码,当MeasureSpec中设置的传入大小为LayoutParams.WRAP_CONTENT或LayoutParams.FILL_PARENT时,我们可能会遇到问题。由于这些有效常量的值分别为-2和-1,因此原始Math.min(*)将变得无用,因为它将在最大大小上保留这些值,并表示测量的WRAP_CONTENT大于我们的最大大小。我想操作员只考虑精确的暗度(它对此很有效)。public MaxWidthLinearLayout扩展LinearLayout { private int mMaxWidth = Integer.MAX_VALUE;public MaxWidthLinearLayout(Context context) { super(context);} public MaxWidthLinearLayout(Context context,AttributeSet attrs) {MaxWidthLinearLayout(context,attrs);TypedArray a= getContext().obtainStyledAttributes(attrs,R.styleable.MaxWidthLinearLayout);mMaxWidth =super Integer.MAX_VALUE);} @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) { super.onMeasure(widthMeasureSpec,);//获取测量高度if(getMeasuredWidth() > mMaxWidth){ setMeasuredDimension(mMaxWidth,getMeasuredHeight());}} }
和xml属性
<!-- MaxWidthLinearLayout -->
<declare-styleable name="MaxWidthLinearLayout">
<attr name="maxWidth" format="dimension" />
</declare-styleable>https://stackoverflow.com/questions/5875877
复制相似问题