为什么GridLayout没有填满所有可用空间?

Lis*_*nne 7 android

我有一个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>
Run Code Online (Sandbox Code Playgroud)

Tom*_*eNS 7

我认为你的布局行为一切都很好.

GridLayoutandroid:layout_width="match_parent"设置(位于Horizo​​ntalScrollView内)无效.插入到GridLayout中的单个子视图确定GridLayout的实际宽度(如果使用垂直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" />
Run Code Online (Sandbox Code Playgroud)

因此,通过更改子视图的宽度,您可以控制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>
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮到你.最好的祝福 :)

另外: 我发现以上可能实际上很难在程序上实现.您可能正在考虑将GridLayout更改为GridView或TableLayout,这些更容易处理.要了解有关它的更多信息,请查看以下网站:

  1. GridLayout.LayoutParams
  2. 网格布局
  3. 网格布局 - 不GridView的 - 如何做 - 拉伸 - 所有的孩子,均匀