android - GridLayout 列超出其范围

标签 android android-layout 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 的范围。

Layout preview with GridLayout selected

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

Layout preview with right column selected

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

Screenshot

这是 GridLayout 中的错误吗?还是我遗漏了什么?

最佳答案

这是 GridLayout“正常” 行为。

幸运的是,有一个新版本的 GridLayout,它是用 API 21 添加的。多亏了这个事实,你可以让 GridLayout 将 child 容纳到它的 宽度或高度取决于它的方向。

要了解详细信息,请查看 documentation ,尤其是在 Class Overview -> Excess Space Distribution。您可以在那里找到如何以您想要的方式使用 GridLayout 的信息。


提示:

不要忘记要使用新的 GridLayout,您需要 add it as support libraryxml 中你应该使用:

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

代替

<GridLayout ... > 

关于android - GridLayout 列超出其范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223523/

相关文章:

android - 如何获取 child 内部的 Uid 和另一个推送 ID child

Android ListView滚动没有动力

android - columnCount 必须大于或等于每个子项的 LayoutParams 中定义的所有网格索引(和跨度)的最大值

android - 为什么 RecyclerView 中的交错网格布局元素在加载图像时会自动交换其位置?

Android CardView 填充和 minHeight

java - android 中的 NullPointerException

android - GC_FOR_ALLOC 释放 6346K, 7% free , paused 143ms, total 143ms

android - RecyclerView 或 ViewPager 或 ViewFlipper

android - 九补丁图像是否可收缩?

Android - 如何将 View 的 x 和 y 坐标放入 gridLayout 中?