我在一个ScrollView里面的一个LinearLayout里面有一个GridView,它可以从服务器中获取数据。在GridView下面是一个加载更多数据的按钮。我的GridView将有一个比屏幕更大的终极高度。如果我将GridView的高度设置为wrap_content或parent_fill,它将自己调整为确切的可用屏幕高度,并且根本不滚动,裁剪出多余的行。如果我明确地将layout_height设置为1000dip之类的东西,滚动行为会正确,但我无法预测我的滚动视图apriori的最终高度。如何计算Android上的ScrollView内的GridView所需的高度?
如何以编程方式确定GridView获得所需行为所需的高度?
这是我的布局如下。正如你可以看到我设置的高度1000dip,但毕竟是假的,我需要的价值得到自动设置/编程:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_weight="1"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="1000dip"
android:columnWidth="70dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#000000"
android:layout_weight="1"
/>
<Button
android:id="@+id/load_more"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load More Foo"
/>
</LinearLayout>
</ScrollView>
仅供参考我确实使用ListView编写了我自己定制的类似GridView的UI小部件,它工作得很好。 – esilver 2012-01-02 22:37:14