【问题标题】:Why GridLayout doesn't fill all the available space?为什么 GridLayout 没有填满所有可用空间?
【发布时间】:2013-09-20 16:30:04
【问题描述】:

我有一个 GridLayout(我以编程方式向其中添加子项)。

结果很丑,因为 GridLayout 没有填满所有可用空间。

这是结果:

这是我的 XML:

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

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.GridLayout
        android:id="@+id/gridlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" >
    </android.support.v7.widget.GridLayout>
</HorizontalScrollView>

</ScrollView>

【问题讨论】:

  • 真诚地,我从来没有在 Horizo​​ntalScrollView 中看到过 GridLayout,都在 ScrollView 中:为什么你需要所有这些滚动?
  • 因为事情可能会变大,这取决于用户输入:-)
  • 尝试添加android:fillViewport="true"并将android:layout_width="match_parent"更改为android:layout_width="wrap_content"。在您的 Horizo​​ntalScrollView 中进行这些更改。

标签: android


【解决方案1】:

我认为您的布局行为一切正常。

GridLayoutandroid:layout_width="match_parent" 设置(位于Horizo​​ntalScrollView 内)无效。插入 GridLayout 的单个子视图确定 GridLayout 的 实际宽度(height 如果您使用垂直 ScrollView 容器),即可以大于父屏幕宽度(width=match_parent 设置因此在这里没有效果;也适用于子视图)。 GridLayout 的列和行具有插入的最大子视图的大小(分配给该列/行)。

这整体是一个非常动态的结构。列数和行数会自动重新计算。请记住使用 layout_rowlayout_column 标记子视图,并可能设置所需的大小 - 遵循上述规则,例如:

        <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:id="@+id/editText1"
        android:layout_row="2"
        android:layout_column="8" />

因此,通过更改子视图的宽度,您可以控制 GridLayout 的列宽度。您可以研究以下示例:

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

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:background="#f3f3f3">

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#a3ffa3">

            <Button
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Col 1,Row4"
                android:id="@+id/button"
                android:layout_gravity="center"
                android:layout_row="4"
                android:layout_column="1" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start.."
                android:layout_column="4"
                android:layout_row="8" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Col 6, Row 1."
                android:id="@+id/button2"
                android:layout_row="1"
                android:layout_column="6" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Col 6, Row 2."
                android:id="@+id/button3"
                android:layout_row="2"
                android:layout_column="6" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button4"
                android:layout_gravity="center"
                android:layout_column="9"
                android:layout_row="3" />

            <TextView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:id="@+id/textView"
                android:layout_row="3"
                android:layout_column="8" />

            <CheckBox
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:text="New CheckBox"
                android:id="@+id/checkBox"
                android:layout_row="6"
                android:layout_column="7"
                android:textColor="#212995" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:ems="10"
                android:id="@+id/editText"
                android:layout_row="5"
                android:layout_column="8"
                android:textColor="#952a30" />

        </GridLayout>
    </HorizontalScrollView>
</LinearLayout>

我希望这会有所帮助。最好的问候:)

另外: 我发现上述内容实际上可能很难以编程方式实现。您可能正在考虑将 GridLayout 更改为更易于处理的 GridView 或 TableLayout。要了解更多信息,请查看以下网站:

  1. GridLayout.LayoutParams
  2. GridLayout
  3. gridlayout-not-gridview-how-to-stretch-all-children-evenly

【讨论】:

    【解决方案2】:

    你应该修改你的

    <HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

    并添加android:fillViewPort="true"。这应该使内容拉伸以填满屏幕(即视口)。

    【讨论】:

      【解决方案3】:

      TextView reps = new TextView(this); android.support.v7.widget.GridLayout.LayoutParams repsLayoutParam = (LayoutParams) reps.getLayoutParams(); repsLayoutParam.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL);

      这将创建一个放置在网格布局中的文本视图 - 将重力设置为 FILL_HORIZONTAL 和 FILL_VERTICAL 应该确保它填充布局中的单元格。

      【讨论】:

        【解决方案4】:

        因为网格视图有自己的滚动条。

        【讨论】:

          【解决方案5】:

          当您添加视图时,请记住首先将正确的LayoutParams 添加到视图中。

          【讨论】:

            【解决方案6】:

            也许是因为失踪了

            </ScrollView>
            

            在最后。

            【讨论】: