为什么 ListView 项目不会增长以包装其内容? [英] Why do ListView items not grow to wrap their content?

查看:32
本文介绍了为什么 ListView 项目不会增长以包装其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的 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.

是否有任何我可能遗漏的与 ListViews 和项目布局以及项目高度相关的问题?

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 上除硬编码像素值之外的任何高度值也是如此.

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) 当父 wraps_content 请求 fill_parent 时,视图的语义是什么?哪个尺寸请求优先?

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?

感谢您的投入.

这篇关于为什么 ListView 项目不会增长以包装其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆