【问题标题】:Android GridView stretch row heightAndroid GridView 拉伸行高
【发布时间】:2017-01-20 15:16:55
【问题描述】:

我有一个GridView,其中的项目没有占满高度。他们希望以与水平方向相同的方式伸展。我能做到吗?

GridView 旨在占用布局中的剩余空间。所以它的高度不同。但它被定义为不可滚动,因为总是有足够的空间。有没有不手动计算每个项目的高度的解决方案?或者如果不是,我可以一般地计算适配器内所需的高度吗?

网格视图定义如下:

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/menu_grid_view"
        android:horizontalSpacing="8dp"
        android:verticalSpacing="8dp"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"/>

我还尝试在项目上使用layout_height=0,layout_weight=1。但这将是一个嵌套的权重,它不起作用。

【问题讨论】:

  • 发布完整的 xml 文件
  • 查看我的其他评论。 xml不是问题。 GridView 上的权重正在工作。网格内的项目没有填满整个空间。 xml 只是一个 LinearLayout,在网格上方和外部有一个按钮,没什么特别的。

标签: android gridview


【解决方案1】:

也许你希望这样做。

权重仅适用于线性布局。

你可以试试这个,我希望这能帮助你理解为什么你没有得到正确布局的问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".3"
        android:text="textview"/>

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".7"
        android:id="@+id/menu_grid_view"
        android:horizontalSpacing="8dp"
        android:verticalSpacing="8dp"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"/>

</LinearLayout>

【讨论】:

  • 抱歉,您理解错了。 GridView 具有正确的大小。它的项目只是不填满空间。所以项目需要在高度上扩展而不是网格本身......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2018-01-31
  • 1970-01-01
相关资源
最近更新 更多