android – GridLayout列超出了它的范围

我正在尝试制作类似网格的形式,类似于 the example on the official Android Developers blog.

这是我的布局:

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

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="48dp"
        android:layout_marginRight="48dp"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:columnCount="2"
        android:rowCount="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_gravity="end"
            android:layout_row="0"
            android:text="Send"
            android:textColor="?android:attr/textColorPrimary"
            android:textSize="24sp" />

        <Spinner
            android:id="@+id/send_currency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_gravity="end"
            android:layout_row="1"
            android:text="to"
            android:textColor="?android:attr/textColorPrimary"
            android:textSize="24sp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="1"
            android:hint="username"
            android:textColor="?android:attr/textColorPrimary"
            android:textSize="24sp" />
    </GridLayout>
</LinearLayout>

我的左栏(静态文本,右对齐)工作正常.它将文本对齐,宽度取决于最宽的行.

但是,右列似乎超出了GridLayout的范围.

在该图片中,蓝色框是GridLayout的边界.您已经可以在顶部的绿色栏中看到问题.右侧应该停在GridLayout的边界(就像左侧一样),但由于某种原因它会超越它.

该图片中的蓝色框是EditText的边界(它设置为wrap_content).然而,浅绿色的盒子是允许它扩展的范围.当我在EditText中输入大量字符时,它会经过GridLayout的边界,甚至超过手机屏幕的边缘!

这是GridLayout中的错误吗?或者我错过了什么?

解决方法

这是GridLayout的“正常”行为.

幸运的是,有一个新版本的GridLayout,它随API 21一起添加.由于这个事实,你可以让GridLayout根据它的方向容纳儿童的宽度或高度.

要了解详细信息,请查看documentation,尤其是课程概述 – >过剩的空间分布.您可以按照自己的方式找到有关如何使用GridLayout的信息.

小费:

不要忘记要使用新的GridLayout,你需要使用add it as support library和xmls,你应该使用:

<android.support.v7.widget.GridLayout ... >

代替

<GridLayout ... >

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...