Android GridLayout向下推其子项,并在为高度指定fill_父项时溢出

Android GridLayout向下推其子项,并在为高度指定fill_父项时溢出,android,view,grid-layout,Android,View,Grid Layout,GridLayout似乎总是将其子对象推到与其需求相对应的布局。例如,以下声明: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android

GridLayout似乎总是将其子对象推到与其需求相对应的布局。例如,以下声明:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="3"
    android:orientation="vertical"
    android:rowCount="4"
    android:useDefaultMargins="true" >
...
    <ImageView
        android:id="@+id/main_image"
        android:layout_column="1"
        android:layout_columnSpan="2"
        android:layout_row="3"
        android:scaleType="fitStart" />
</GridLayout>

...
GridLayout声明fill_parent,因此我希望它不会溢出。GridLayout应该采用父级的大小,在本例中,父级是窗口(全高)。但是,在层次查看器中,GridLayout被设置为垂直和水平方向的包裹内容

因此,ImageView(大图像)或任何文本视图将被推送以适合自己,从而溢出容器

这可以在层次结构查看器中看到,其中容器网格视图适合父对象:

而图像视图溢出

通过阅读文档,我了解到需要设置重力。就我所能尝试的而言,我使用了各种各样的重力选项和图像缩放选项,但没有太大效果。使用
useDefaultMargins=“false”
删除页边距确实会改变版面溢出,从而将问题导向gridlayout

我的问题如下:

  • 这是一个bug还是我使用的GridLayout不正确
  • 我如何才能强制GridLayout的子项适应其容器并填充 剩余空间

其他布局的诀窍是给第一个元素一个android:layout\u weight=“1.0”,其他元素什么都不给。我不知道它为什么起作用,但它确实起作用了。下面是一个简单的XML,它显示了一个ImageView、一个TextView和一个按钮。在没有为ImageView指定layout_weight参数的情况下,文本和按钮向下移动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical"
    android:keepScreenOn = "true" >

    <ImageView
        android:id="@+id/imageView_surprise"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:scaleType="centerInside"
        android:contentDescription="@string/accessibility_imageView_surprise"
        android:layout_weight="1.0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom" >

        <TextView
            android:id="@+id/textView_message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="24sp"
            android:gravity="center_horizontal" />

        <Button
            android:id="@+id/button_share"
            style="@style/button_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

看到这一点,我也有同样的问题,除非我给第一个元素一个明确的高度,否则它会占据整个父元素并推动曲面的其余部分。。。