【问题标题】:Expandable GridView height does not expand to the content's heightExpandable GridView 高度不会扩展到内容的高度
【发布时间】:2013-09-20 06:15:02
【问题描述】:

我正在使用这个ExpandableHeightGridView 类,其中GridView"supposedly" 延伸到内容的高度,因为我将它放在ScrollView 中。如果我的GridViewitems 是完整的,这很好用,但由于我的GridViewitems 是动态的,所以它的项目数量并不总是相同,现在的问题是如果我的GridViewitems 较少,它的高度不会换行到内容高度,但有 blank 行的空间。我不明白它为什么分配那个 blank 行。

这是布局:

<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。我说对了吗?

【问题讨论】:

    标签: android android-gridview android-scrollview expandable row-height


    【解决方案1】:

    尝试在您的活动中添加这个。

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

    我基于您的link

    【讨论】:

    • 我已经应用了exGridView.setExpanded(true); 使其拉伸,但它拉伸到了整个高度,而不是GridView 的实际高度。例如,我的最大 GridView 行是 4,但如果我只有 2 行,它会再分配 2 个空白行。
    【解决方案2】:

    当你想给 layout_weight 时,请确保 layout_height 为 0dp。不要同时设置 layout_height 和 layout_weight。希望对你有所帮助。

    同时从滚动视图中移除这个标签 android:fillViewport="true"。您实际上是在通过将 fillViewport 设置为 true 来要求滚动视图扩展网格视图是否具有较少的项目。如果您删除此标签,则 gridview 将换行至其内容,并且行下方不会有多余的空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多