我正在尝试制作类似网格的表格,类似于 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
将 child 容纳到它的 宽度或高度取决于它的方向。
要了解详细信息,请查看 documentation ,尤其是在 Class Overview -> Excess Space Distribution。您可以在那里找到如何以您想要的方式使用 GridLayout
的信息。
提示:
不要忘记要使用新的 GridLayout
,您需要 add it as support library在 xml
中你应该使用:
<android.support.v7.widget.GridLayout ... >
代替
<GridLayout ... >
关于android - GridLayout 列超出其范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223523/