本文介绍了Android:为什么视图没有 maxHeight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要实现的是让一些项目(视图)填充 ScrollView.当有 1..3 个项目时,我想直接显示它们.这意味着 ScrollView 具有 1、2 或 3 个项目的高度.

What I'm trying to achieve is having some items (views) filling up a ScrollView. When there are 1..3 items I want to display them directly. Meaning the ScrollView has the height of either 1, 2 or 3 items.

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

When there are 4 or more items I want the ScrollView to stop expanding (thus a maxHeight) and start providing scrolling.

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

However, there is unfortunately no way to set a maxHeight. So I probably have to set my ScrollView height programmatically to either WRAP_CONTENT when there are 1..3 items and set the height to 3*sizeOf(View) when there are 4 or more items.

谁能解释为什么没有提供maxHeight,而已经有minHeight?

Can anyone explain why there is no maxHeight provided, when there is already a minHeight?

(顺便说一句:某些视图,例如 ImageView 实现了 maxHeight.)

(BTW: some views, like ImageView have a maxHeight implemented.)

推荐答案

这些解决方案都不能满足我的需要,即 ScrollView 设置为 wrap_content 但具有 maxHeight,因此它会在某个点后停止扩展并开始滚动.我只是简单地覆盖了 ScrollView 中的 onMeasure 方法.

None of these solutions worked for what I needed which was a ScrollView set to wrap_content but having a maxHeight so it would stop expanding after a certain point and start scrolling. I just simply overrode the onMeasure method in ScrollView.

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

这可能不适用于所有情况,但它确实为我提供了布局所需的结果.它还解决了 madhu 的评论.

This might not work in all situations, but it certainly gives me the results needed for my layout. And it also addresses the comment by madhu.

如果滚动视图下方存在某些布局,则此技巧将不起作用 – madhu 3 月 5 日 4:36

这篇关于Android:为什么视图没有 maxHeight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 07:16