Cri*_*ian 4 user-interface android listview
我有三个ListView小部件LinearLayout.像这样的东西(我省略了在这个例子中不相关的XML元素):
<LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="360dp"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="360dp"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="360dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这会强制列表的高度为360度.当然,即使列表项很少,这也将是它的高度.所以,我的问题是如何使列表具有自动高度?我想要的是ListView高度采用其所有列表项的总和的确切大小.
我已经用这种方式实现了它(代码正在进行中,所以它更像是一个想法源而不是解决方案):
package com.customcontrols;
public class NoScrollListView extends ListView
{
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0) );
// here I assume that height's being calculated for one-child only, seen it in ListView's source which is actually a bad idea
int childHeight = getMeasuredHeight() - (getListPaddingTop() + getListPaddingBottom() + getVerticalFadingEdgeLength() * 2);
int fullHeight = getListPaddingTop() + getListPaddingBottom() + childHeight*(getCount());
setMeasuredDimension(getMeasuredWidth(), fullHeight);
}
}
Run Code Online (Sandbox Code Playgroud)
计算并不完美,但它已经接近并且到目前为止仍然有效.之后你只需创建一个这样的布局:
ScrollView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView com.customcontrol.NoScrollListView/ScrollView
scrollView是至关重要的,因为您可以轻松地用完屏幕边界.
PS.计算的直肠驱动因为ListView&Co中的大多数计算方法都是包私有的,这对于UI的公共可继承类来说是一个非常奇怪的选择.
| 归档时间: |
|
| 查看次数: |
9009 次 |
| 最近记录: |