我在 ScrollView 内的 LinearLayout 内有一个 GridView,用于从服务器中分页数据。 GridView 下方是一个用于加载更多数据的按钮。我的 GridView 的最终高度将大于屏幕。如果我将 GridView 的高度设置为wrap_content 或parent_fill,它会将自身调整为屏幕上可用的确切高度,并且根本不滚动,从而裁剪掉多余的行。如果我明确地将layout_height设置为较大的值,例如1000dip,则滚动行为正常,但是我无法先验地预测滚动视图的最终高度。
如何以编程方式确定 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>
如果有人需要的话,这是一种方法。有点黑客行为,但确实有效。您必须将 GridView 初始设置为足够大以容纳所有视图(例如 10000dip)
final GridView imageContainer = // your GridView
imageContainer.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener( this );
View lastChild = imageContainer.getChildAt( imageContainer.getChildCount() - 1 );
imageContainer.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)