可扩展的GridView高度不会随内容高度而扩展。

4
我正在使用这个ExpandableHeightGridView类,其中GridView"理论上"应该根据内容高度自动调整高度,因为我把它放在了一个ScrollView中。 如果我的GridView项完整的话,这个方法就能正常工作,但是,由于我的GridView项是动态的,所以它们并不总是有相同数量的项,现在问题是,如果我的GridView项较少,则其高度不会自适应内容高度,而是占用了一行空白。 我不理解为什么它需要分配那一行空白

下面是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="@string/hello_world" />

        <com.example.ExpandableHeightGridView
            android:id="@+id/gridview_module"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:horizontalSpacing="20dp"
            android:isScrollContainer="false"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

        <Button
            android:id="@+id/dial_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:onClick="dialPhone"
            android:text="Dial Phone" />
    </LinearLayout>
</ScrollView>

我把GridView的权重设置为1,其他的都是0。我的理解正确吗?

2个回答

2
尝试将这个添加到您的活动中。
ExpandableHeightGridView exGridView;

exGridView = (ExpandableHeightGridView) findViewById(R.id.myId);
exGridView .setExpanded(true);

我是基于你的链接来完成翻译的。


我已经应用了 exGridView.setExpanded(true); 使其拉伸,但它会拉伸到整个高度而不是实际的 GridView 高度。例如,我的最大 GridView 行数为4,但如果只有2行,它会分配2行空白行。 - Compaq LE2202x

0

当您想要设置layout_weight时,请确保layout_height为0dp。不要同时设置layout_height和layout_weight。希望这可以帮到您。

另外,从滚动视图中删除android:fillViewport="true"标记。实际上,您正在要求滚动视图通过将fillViewport设置为true来扩展gridview(如果它具有较少的项目)。如果您删除此标记,则gridview将包装其内容,并且行下方将没有额外的空间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接