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

标签 android

我有一个 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>

最佳答案

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

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

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

        <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" />

因此,通过更改 subview 的宽度,您可以控制 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

关于android - 为什么 GridLayout 没有填满所有可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921587/

相关文章:

android - Honeycomb android :hardwareAccelerated ="true" manifest setting apply for Live Wallpapers?是否

Android Intent with Cordova - 每次分享都会启动新的应用程序实例。应该只有一个

java - 如何将(代码生成的)TableRow 的内容与字符串进行比较?

android - 在 Android 中显示包含不同类型信息的列表

Android 测量屏幕的百分比

java - 将InputStream的内容打印到LogCat

php - Json 不会在 php 中响应我的数组

java - 使用 Android API 发布推文

android - 如何获取传入 xmpp 消息的时间戳?

android - Kotlin 协程和 SecurityException