【问题标题】:GridLayout column is going beyond its boundsGridLayout 列超出了它的界限
【发布时间】:2015-05-27 05:26:15
【问题描述】:

我正在尝试制作类似于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 中的错误吗?还是我错过了什么?

【问题讨论】:

  • 您找到解决方案了吗?

标签: android android-layout android-gridlayout


【解决方案1】:

这是GridLayout'正常'行为。

幸运的是,GridLayout 有一个新版本,它是在 API 21 中添加的。因此,您可以让GridLayout 适应其宽度或高度它的方向。

要了解详细信息,请查看documentation,尤其是类概述 -> 多余空间分布。您可以在那里找到如何以您想要的方式使用GridLayout 的信息。


提示:

别忘了要使用新的GridLayout,你需要add it as support library,在xmls你应该使用:

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

而不是

<GridLayout ... > 

【讨论】:

  • 添加支持gridlayout并没有带来解决方案。是否有我们应该设置的特定属性?
【解决方案2】:

我发现this answer 很有帮助。另外,paulina_glab 的回答是使用 &lt;android.support.v7.widget.GridLayout 而不是 &lt;GridLayout - 谢谢!

尤其是每个单元格上的这些属性:

android:layout_width="0dp"
app:layout_columnSpan="1"
app:layout_columnWeight="1"
app:layout_rowSpan="1"

希望对你有帮助。

【讨论】:

    【解决方案3】:

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

    <EditText 
    ...
    android:singleLine="true"
    android:lines="1"
    android:maxLines="1"
    android:hint="to"></EditText>
    

    【讨论】:

    • 它仍然过度向右扩展边界,所以这并不能解决问题。
    【解决方案4】:

    确实很奇怪。如果将 EditText 的宽度限制为“match_parent”而不是“wrap_content”(应该将其水平宽度限制在网格单元格内),会发生什么?

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

    或者,或者,给你的 EditText 一个右边距来尝试推动右边的边框一点点?例如

    android:layout_marginRight="20dp"
    

    看看会不会有什么不同?

    【讨论】:

    • 将其更改为match_parent 仍然存在相同的溢出问题,唯一的区别是它开始比屏幕宽。
    • 边距确实修复了它,但它很hacky,必须检查第一列中字符串的每个翻译。绝对不是一个好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2013-03-08
    相关资源
    最近更新 更多