视图有minHeight,但不知何故缺少maxHeight:

我要实现的是有一些项目(视图)填充一个ScrollView。当有1..3个项目,我想直接显示他们。意思是ScrollView有1,2或3个项的高度。

当有4个或更多的项目时,我希望ScrollView停止展开(因此是maxHeight)并开始提供滚动。

然而,不幸的是,没有办法设置maxHeight。所以我可能需要通过编程方式将ScrollView的高度设置为WRAP_CONTENT当有1。当有4个或更多的项目时,设置高度为3*sizeOf(视图)。

有人能解释为什么没有maxHeight提供,当已经有一个minHeight?

(BTW:一些视图,如ImageView有一个maxHeight实现。)


当前回答

我有一个答案:

https://stackoverflow.com/a/29178364/1148784

只需创建一个扩展ScrollView的新类并覆盖它的onMeasure方法。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0){
            int hSize = MeasureSpec.getSize(heightMeasureSpec);
            int hMode = MeasureSpec.getMode(heightMeasureSpec);

            switch (hMode){
                case MeasureSpec.AT_MOST:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.UNSPECIFIED:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
                    break;
                case MeasureSpec.EXACTLY:
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
                    break;
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

其他回答

如果你想做一个无溢出的scrollview或listview,只要把它放在一个RelativeLayout上,上面和下面都有topview和bottomview:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/topview"
    android:layout_below="@+id/bottomview" >

如果有人正在考虑使用LayoutParams的确切值。

setLayoutParams(new LayoutParams(Y, X );

记住要考虑到设备显示的密度,否则你可能会在不同的设备上看到非常奇怪的行为。例句:

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics d = new DisplayMetrics();
display.getMetrics(d);
setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, (int)(50*d.density) ));

这些解决方案都没有为我所需要的是一个ScrollView设置为wrap_content,但有一个maxHeight,因此它将在某一点后停止展开并开始滚动。我只是简单地覆盖了ScrollView中的onMeasure方法。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(300, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

这可能不是在所有情况下都有效,但它确实为我的布局提供了所需的结果。它也提到了madhu的评论。

如果在滚动视图下面出现一些布局,那么这个技巧将不起作用- madhu Mar 5 at 4:36

用layout_height="max_height"包装你的ScrollView围绕你的plainLinearLayout,这将做一个完美的工作。事实上,我在过去5年里一直在生产中使用这个代码,没有任何问题。

<LinearLayout
        android:id="@+id/subsParent"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical">

        <ScrollView
            android:id="@+id/subsScroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="15dp"
            android:layout_marginStart="15dp">

            <TextView
                android:id="@+id/subsTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/longText"
                android:visibility="visible" />

        </ScrollView>
    </LinearLayout>

(我知道这并没有直接回答问题,但可能有助于其他人寻找maxHeight功能)

ConstraintLayout为其子via提供了最大高度

app:layout_constraintHeight_max="300dp"
app:layout_constrainedHeight="true" 

or

app:layout_constraintWidth_max="300dp"
app:layout_constrainedWidth="true" 

这里是示例用法。