视图有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实现。)


当前回答

以防有人需要:

app:layout_constraintHeight_max="300dp"

它强制视图(在ConstraintLayout中)的最大高度为300dp。对于那些想要通过编程来实现这一点的人来说,它是这样的:

val totalScreenHeight = displayMetrics.heightPixels
val layoutParams: ConstraintLayout.LayoutParams = viewThatIsInsideAConstraintLayout.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintMaxHeight = totalScreenHeight/2
viewThatIsInsideAConstraintLayout.layoutParams = layoutParams

其他回答

这些解决方案都没有为我所需要的是一个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

正如我们所知,运行android的设备可以有不同的屏幕尺寸。我们进一步知道,景观应该动态调整,成为合适的空间。

如果你设置了最大高度,你可能会迫使视图没有足够的空间或占用更少的空间。我知道有时候设置一个最大高度是很实际的。但如果分辨率会发生巨大变化,它会的!,则具有最大高度的视图将看起来不合适。

我认为没有正确的方法来精确地做你想要的布局。我建议你使用布局管理器和相关机制来考虑你的布局。我不知道你想要达到什么目的,但这听起来有点奇怪,一个列表应该只显示三个项目,然后用户必须滚动。

顺便说一句。minHeight不能保证(也许也不应该存在)。当其他相关的项目变得更小的时候,强制项目可见会有一些好处。

首先获取项目高度(以像素为单位)

View rowItem = adapter.getView(0, null, scrollView);   
rowItem.measure(0, 0);    
int heightOfItem = rowItem.getMeasuredHeight();

那么简单的

Display display = getWindowManager().getDefaultDisplay();    
DisplayMetrics displayMetrics = new DisplayMetrics();    
display.getMetrics(displayMetrics);    
scrollView.getLayoutParams().height = (int)((heightOfItem * 3)*displayMetrics .density);

我使用了一个在Kotlin中使用maxHeight的自定义ScrollView。使用示例:

<com.antena3.atresplayer.tv.ui.widget.ScrollViewWithMaxHeight
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="100dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</com.antena3.atresplayer.tv.ui.widget.ScrollViewWithMaxHeight>

下面是ScrollViewWidthMaxHeight的代码:

import android.content.Context
import android.util.AttributeSet
import android.widget.ScrollView
import timber.log.Timber

class ScrollViewWithMaxHeight @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {

companion object {
    var WITHOUT_MAX_HEIGHT_VALUE = -1
}

private var maxHeight = WITHOUT_MAX_HEIGHT_VALUE

init {
    val a = context.obtainStyledAttributes(
        attrs, R.styleable.ScrollViewWithMaxHeight,
        defStyleAttr, 0
    )
    try {
        maxHeight = a.getDimension(
            R.styleable.ScrollViewWithMaxHeight_android_maxHeight,
            WITHOUT_MAX_HEIGHT_VALUE.toFloat()
        ).toInt()
    } finally {
        a.recycle()
    }
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    var heightMeasure = heightMeasureSpec
    try {
        var heightSize = MeasureSpec.getSize(heightMeasureSpec)
        if (maxHeight != WITHOUT_MAX_HEIGHT_VALUE) {
            heightSize = maxHeight
            heightMeasure = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST)
        } else {
            heightMeasure = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.UNSPECIFIED)
        }
        layoutParams.height = heightSize
    } catch (e: Exception) {
        Timber.e(e, "Error forcing height")
    } finally {
        super.onMeasure(widthMeasureSpec, heightMeasure)
    }
}

fun setMaxHeight(maxHeight: Int) {
    this.maxHeight = maxHeight
}
}

这也需要在values/attrs.xml中声明:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ScrollViewWithMaxHeight">
        <attr name="android:maxHeight" />
    </declare-styleable>
</resources>

为了创建一个带有maxHeight的ScrollView或ListView,你只需要在它周围创建一个透明的线性布局,它的高度是你想要的maxHeight。然后将ScrollView的高度设置为wrap_content。这将创建一个ScrollView,它看起来会增长,直到它的高度等于父LinearLayout。