【发布时间】:2015-07-14 05:06:55
【问题描述】:
拥有listView,如果内容较少,则其高度应与"wrap_content" 一致。如果它有更多行,则最大高度应限制在某个高度。
ListView中允许设置android:maxHeight:
<ListView>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="120dp"
</ListView>
但它不起作用并且总是"wrap_content"。
让它工作的唯一方法是在代码使用中
int cHeight = parentContainer.getHeight();
ViewGroup.LayoutParams lp = mListView.getLayoutParams();
if (messageListRow > n)
{
lp.height = (int)(cHeight * 0.333);
}
else
{
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
mListView.setLayoutParams(lp);
有没有办法在xml中做到这一点?
【问题讨论】:
-
不要使用“wrap_content”作为列表视图的高度,因为它会导致你的适配器被多次调用。你为什么不把你的listview放在一个LinearLayout容器中,然后根据你的需要控制高度。
-
好点,谢谢 Pztar。但是即使放在一个 LinearLayout 容器中,定义容器的高度仍然是个问题。也许在代码中动态设置是唯一的方法。
标签: android android-listview height