为什么ListView的项目不能生长来包装他们的内容?他们的、生长、项目、内容

2023-09-12 22:44:08 作者:笑容背后

我有一个相当复杂的ListView,与变量列表项的高度。在某些情况下,我需要显示在列表中的项目,它默认是隐藏的(View.GONE)的额外视图。通过启用它(View.VISIBLE),列表项生长在高(或至少它应该)。

I have a rather complex ListView, with variable list item heights. Under certain conditions, I need to display an additional view in a list item, which is hidden by default (View.GONE). By enabling it (View.VISIBLE), the list item grows in height (or at least it's supposed to).

问题: 即使我宣布该项目的根布局WRAP_CONTENT,并在项目的每个组件FILL_PARENT,我隐藏视图/显示这是应该改变项目的高度只需在底部,而不是它的父(项目布局)切断在高度成长充分显示出来。

The problem: Even though I declare the item's root layout to wrap_content, and each component in the item to fill_parent, the view I hide/show which is supposed to change the item's height is simply cut off at the bottom instead of its parent (the item layout) growing in height to fully display it.

是否有相关的列表视图和项目布局和项目的高度,我可能已经错过了?任何陷阱

Are there any gotchas related to ListViews and item layouts and item height which I may have missed?

有更多的观察:

有关测试的目的,我现在已经减少了列表项的布局只包含根的LinearLayout和ImageView的。当我设置的LinearLayout高度例如200dip和ImageView的到FILL_PARENT,我本来期望ImageView的增长,直到它击中200dip限制其母公司设置。

For testing purposes I have now reduced the list item layout to just contain the root LinearLayout and an ImageView. When I set the LinearLayout height to e.g. 200dip and the ImageView to fill_parent, I would have expected the ImageView to grow until it hits the 200dip limit set by its parent.

不过,图像将改为永远只能那么高,它的位图资源(我仿佛将其设置为WRAP_CONTENT),整个列表项将是相同的高度(即,如果我将它设置为WRAP_CONTENT,太)。

However, the image will instead be only ever as tall as its bitmap resource (as if I had set it to wrap_content) and the whole list item will be of the same height (i.e. as if I had set it to wrap_content, too).

然而,如果我设置图像的高度,例如200dip,则列表项将生长在高度,所以将项目布局。

If however I set the image height to e.g. 200dip, then the list item will grow in height, and so will the item layout.

在换句话说,列表项目布局layout_height完全被忽略,所以是在ImageView的任何高度值比硬codeD像素值等。

In other words, the layout_height of the list item layout is completely ignored, and so is any height value on ImageView other than a hard-coded pixel value.

推荐答案

我设法解决这个问题,但我不明白的为什么的。

I managed to fix this, but I don't understand why.

正如我所说,我已经设置了 layout_height 的列表项目布局 WRAP_CONTENT (因为 FILL_PARENT 是没有意义的位置,考虑到一个ListView是无限期高大)。

As I mentioned, I had set the layout_height of the list item layout to wrap_content (since fill_parent is meaningless here, considering that a ListView is indefinitely tall).

不过,我已经设置了 layout_height 的所有视图的在的是布局,以 FILL_PARENT 。他们设置的时候问题就消失了 WRAP_CONTENT 代替。

However, I had set the layout_height of all views inside that layout to fill_parent. The problem disappeared when setting them to wrap_content instead.

这引起了其他两个问题:

This raises two other questions:

1)什么是一个视图要求为 FILL_PARENT 的语义,当父 wraps_content ?其中大小请求需要precedence?

1) What are the semantics of a view asking to fill_parent, when the parent wraps_content? Which size request takes precedence?

2)如何我会永远做一个视图填充列表项,如果 FILL_PARENT 显然不起作用?

2) How would I ever make a view fill a list item if fill_parent apparently doesn't work?

感谢您输入家伙。