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

10

我有一个GridLayout(我通过编程向其中添加子项)。

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

这是最终的效果:

enter image description here

这是我的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>

1
真诚地说,我从未见过在ScrollView中同时使用HorizontalScrollView和GridLayout:你为什么需要这么多滚动? - JJ86
因为这个东西可能会根据用户输入变得很大 :-) - Lisa Anne
1
尝试在HorizontalScrollView中添加android:fillViewport="true",并将android:layout_width="match_parent"更改为android:layout_width="wrap_content" - Sherif elKhatib
6个回答

8
我认为你的布局行为一切正常。
HorizontalScrollView中放置的GridLayout,设置android:layout_width="match_parent"并不起作用。单个插入到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" />

所以,通过改变子视图的宽度,您可以控制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

4
你应该修改你的 <\p>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

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


2

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应该确保它填充了布局中的单元格。


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

1
当您添加视图时,请先为视图添加正确的LayoutParams

0

可能是因为缺失了

</ScrollView>

在结尾处。


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