首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ViewGroup上设置最大宽度

在ViewGroup上设置最大宽度
EN

Stack Overflow用户
提问于 2011-05-04 05:23:43
回答 4查看 40.4K关注 0票数 60

如何设置ViewGroup的最大宽度?我使用的是Theme.Dialog activity,然而,当调整到更大的屏幕时,它看起来不是很好,它也是一种轻量级的东西,我不希望它占据整个屏幕。

我尝试了this suggestion,但没有用。而且,没有像某些视图那样的android:maxWidth属性。

有没有办法限制根LinearLayout,使其仅为(例如) 640个dip?为此,我愿意更改为另一个ViewGroup。

有什么建议吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-06-02 16:24:31

我所做的一种选择是扩展LinearLayout并覆盖onMeasure函数。例如:

代码语言:javascript
复制
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将使用自定义类:

代码语言:javascript
复制
<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文件条目

代码语言:javascript
复制
<declare-styleable name="BoundedView">
    <attr name="bounded_width" format="dimension" />
    <attr name="bounded_height" format="dimension" />
</declare-styleable>

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

票数 89
EN

Stack Overflow用户

发布于 2012-07-26 00:29:10

下面是Dori的答案的更好的代码。

onMeasure方法中,如果你首先在方法中调用super.onMeasure(widthMeasureSpec, heightMeasureSpec);,那么布局中所有对象的宽度都不会改变。因为它们是在设置布局(父)宽度之前初始化的。

代码语言:javascript
复制
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/

感谢你的问题和答案。你的回答对我帮助很大,我希望将来也能对其他人有所帮助。

票数 31
EN

Stack Overflow用户

发布于 2017-06-03 01:12:31

现在android.support.constraint.ConstraintLayout让这一切变得更容易了。只需用ConstraintLayout包装您的视图(任何类型),并为视图设置以下属性:

代码语言:javascript
复制
android:layout_width="0dp"
app:layout_constraintWidth_default="spread"
app:layout_constraintWidth_max="640dp"

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

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5875877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档