Android: 为什么在视图中没有 maxHeight?

视图有一个 minHeight ,但不知为何缺少一个 maxHeight:

我试图实现的是有一些项目(视图)填补了 ScrollView。当有一个的时候。.3项我想直接显示它们。意味着 ScrollView有1、2或3个项目的高度。

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

然而,不幸的是没有办法设置 maxHeight。因此,我可能必须设置我的 ScrollView高度编程为 WRAP_CONTENT时,有1。.3个项目,并设置高度为 3*sizeOf(View)时,有4个或更多的项目。

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

(顺便说一句: 有些视图,比如 ImageView,实现了 maxHeight。)

149179 次浏览

你试过使用layout_weight价值吗?如果将其中一个设置为大于0的值,它将把该视图扩展到剩余的可用空间中。

如果您有多个视图需要拉伸,那么值将成为它们之间的权重。

因此,如果你有两个视图都设置为layout_weight值为1,那么它们都会拉伸来填充空间,但它们都会拉伸到等量的空间。如果你将其中一个的值设为2,那么它的伸缩范围将是另一个视图的两倍。

线性布局下面列出了更多信息

我认为你可以在运行时为1项设置高度scrollView.setHeight(200px),为2项设置高度scrollView.setheight(400px)为3或更多scrollView.setHeight(600px)

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

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

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

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

没有办法设置maxHeight。但是你可以设置高度。

要做到这一点,你需要发现你的scrollView的每个项目的高度。之后,设置你的scrollView高度为numberOfItens * hightofitem。

要发现一个项目的高度,可以这样做:

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

要设置高度,请执行以下操作:

// if the scrollView already has a layoutParams:
scrollView.getLayoutParams().height = heightOfItem * numberOfItens;
// or
// if the layoutParams is null, then create a new one.
scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, heightOfItem * numberOfItens));

如果有人正在考虑使用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) ));

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

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

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

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);

如果你想做一个无溢出的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" >

这对我来说是有效的,使它在xml中可定制:

MaxHeightScrollView.java:

public class MaxHeightScrollView extends ScrollView {


private int maxHeight;
private final int defaultHeight = 200;


public MaxHeightScrollView(Context context) {
super(context);
}


public MaxHeightScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
init(context, attrs);
}
}


public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (!isInEditMode()) {
init(context, attrs);
}
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
if (!isInEditMode()) {
init(context, attrs);
}
}


private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
//200 is a defualt value
maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, defaultHeight);


styledAttrs.recycle();
}
}


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

attr.xml

<declare-styleable name="MaxHeightScrollView">
<attr name="maxHeight" format="dimension" />
</declare-styleable>

示例布局

<blah.blah.MaxHeightScrollView android:layout_weight="1"
app:maxHeight="90dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/commentField"
android:hint="Say Something"
android:background="#FFFFFF"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_vertical"
android:maxLines="500"
android:minHeight="36dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</blah.blah.MaxHeightScrollView>

如果可以的话,我会对whizzle的回答进行评论,但我认为为了让我在Android N的多窗口模式下解决这个问题,我需要稍微更改代码如下:

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

这允许布局调整到小于最大高度,但也防止它大于最大高度。我使用这是一个覆盖RelativeLayout的布局类,这允许我创建一个自定义对话框,其中ScrollView作为MaxHeightRelativeLayout的子对话框,它不会扩展屏幕的整个高度,也会缩小到适合Android N多窗口的最小寡妇大小。

MaxHeightScrollView自定义视图

public class MaxHeightScrollView extends ScrollView {
private int maxHeight;


public MaxHeightScrollView(Context context) {
this(context, null);
}


public MaxHeightScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}


private void init(Context context, AttributeSet attrs) {
TypedArray styledAttrs =
context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
try {
maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_mhs_maxHeight, 0);
} finally {
styledAttrs.recycle();
}
}


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

style.xml

<declare-styleable name="MaxHeightScrollView">
<attr name="mhs_maxHeight" format="dimension" />
</declare-styleable>

使用

<....MaxHeightScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:mhs_maxHeight="100dp"
>


...


</....MaxHeightScrollView>

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

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

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

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

示例用法在这里

用layout_height="max_height"将你的ScrollView包裹在你的普通__abc1周围,这将做一个完美的工作。事实上,我在过去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>

我有一个答案:

https://stackoverflow.com/a/29178364/1148784 < a href = " 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);
}

如上所述,ConstraintLayout通过以下方式为其子节点提供最大高度:

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

此外,如果一个ConstraintLayout的子节点的最大高度在App运行之前是不确定的,那么仍然有一种方法可以让这个子节点自动适应一个可变的高度,无论它被放置在垂直链的哪个位置。

例如,我们需要显示一个底部对话框,一个可变的头部TextView,一个可变的ScrollView和一个可变的页脚TextView。对话框的最大高度是320dp,当总高度没有达到320dp时,ScrollView作为wrap_content,当总高度超过ScrollView时,作为“maxHeight=320dp -头高-脚高”。

我们可以通过xml布局文件来实现这一点:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="320dp">


<TextView
android:id="@+id/tv_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_10"
android:gravity="center"
android:padding="10dp"
app:layout_constraintBottom_toTopOf="@id/scroll_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:layout_constraintVertical_chainStyle="packed"
tools:text="header" />


<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_30"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/tv_footer"
app:layout_constraintHeight_max="300dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_header">


<LinearLayout
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<TextView
android:id="@+id/tv_sub1"
android:layout_width="match_parent"
android:layout_height="160dp"
android:gravity="center"
android:textColor="@color/orange_light"
tools:text="sub1" />


<TextView
android:id="@+id/tv_sub2"
android:layout_width="match_parent"
android:layout_height="160dp"
android:gravity="center"
android:textColor="@color/orange_light"
tools:text="sub2" />


</LinearLayout>
</ScrollView>


<TextView
android:id="@+id/tv_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_50"
android:gravity="center"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/scroll_view"
tools:text="footer" />
</android.support.constraint.ConstraintLayout>

大多数导入代码都很短:

app:layout_constraintVertical_bias="1"
app:layout_constraintVertical_chainStyle="packed"


app:layout_constrainedHeight="true"

横向maxWidth的使用是完全相同的。

我使用了在Kotlin中使用maxHeight自定义滚动视图。使用示例:

<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>

以防有人需要:

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