安卓:为什么没有对了maxHeight视图?视图、对了、maxHeight

由网友(伤心绝望的,对爱情绝望的)分享简介:查看的有了minHeight 但不知何故,缺乏一个了maxHeight :我试图做到的,是有一些项目(视图)填补了一个滚动型。当有1..3项目我想直接显示出来。含义滚动型有一个1,2或3个项目的高度。What I'm trying to achieve is having some items (views)...

查看的有了minHeight 但不知何故,缺乏一个了maxHeight

我试图做到的,是有一些项目(视图)填补了一个滚动型。当有1..3项目我想直接显示出来。含义滚动型有一个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个或更多的项目我想滚动型停止扩张(因而了maxHeight ),并开始提供滚动。

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

然而,不幸的是没有办法设置一个了maxHeight 。所以,我可能要编程设置我的滚动型高度为 WRAP_CONTENT 当有1..3项目和设置高度 3 *整型尺寸(查看)时,有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?

(BTW:一些观点,如的ImageView 有一个了maxHeight 实施。)

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

推荐答案

所有这些解决方案的工作是什么,我需要这是一个滚动型设置为WRAP_CONTENT但有一个了maxHeight,因此将停止某一点后扩大,并开始滚动。我只是单纯地推翻,滚动型的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);
}

这可能不是在所有情况下都有效,但它肯定给了我需要我的布局的结果。而且这还解决了评论意见马杜。

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.

如果一些布局present滚动视图下方则这一招不会工作 - 马杜3月5日在4:36

If some layout present below the scrollview then this trick wont work – madhu Mar 5 at 4:36

阅读全文

相关推荐

最新文章