GridLayout列超出了它的范围

Ste*_*oen 8 android android-layout android-gridlayout

我正在尝试制作类似网格的形式,类似于官方Android开发者博客上的示例.

这是我的布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

但是,右列似乎是在GridLayout范围之外绘制的.

选择了GridLayout的布局预览

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

选择了右列的布局预览

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

截图

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

pau*_*lab 7

这是'正常'的行为GridLayout.

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

要了解详细信息,请查看文档,尤其是类概述 - >超额空间分布.您可以GridLayout按照自己想要的方式找到有关如何使用的信息.


小费:

不要忘记要使用new GridLayout,你需要将它添加为支持库,xml你应该使用:

<android.support.v7.widget.GridLayout ... >
Run Code Online (Sandbox Code Playgroud)

代替

<GridLayout ... > 
Run Code Online (Sandbox Code Playgroud)

  • 添加支持gridlayout不会带来解决方案.是否有我们应该设置的特定属性? (3认同)

RBK*_*RBK 1

您可以将编辑文本限制为单行。

<EditText 
...
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:hint="to"></EditText>
Run Code Online (Sandbox Code Playgroud)

  • 它仍然过度向右扩展边界,所以这并不能解决问题。 (3认同)