簡體   English   中英

GridLayout列超出了它的范圍

[英]GridLayout column is going beyond its bounds

我正在嘗試制作類似網格的形式,類似於官方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>

我的左欄(靜態文本,右對齊)工作正常。 它將文本對齊,寬度取決於最寬的行。

但是,右列似乎是在GridLayout范圍之外繪制的。

選擇了GridLayout的布局預覽

在該圖片中,藍色框是GridLayout的邊界。 您已經可以在頂部的綠色欄中看到問題。 右側應該停在GridLayout的邊界(就像左側一樣),但由於某種原因它會超越它。

選擇了右列的布局預覽

該圖片中的藍色框是EditText的邊界(它設置為wrap_content)。 然而,淺綠色的盒子是允許它擴展的范圍。 當我在EditText中輸入大量字符時,它會經過GridLayout的邊界,甚至超過手機屏幕的邊緣!

截圖

這是GridLayout中的錯誤嗎? 或者我錯過了什么?

這是GridLayout “正常”行為。

幸運的是,有一個新版本的GridLayout ,它隨API 21一起添加。由於這個事實,你可以使GridLayout根據其方向容納兒童的寬度或高度

要了解詳細信息,請查看文檔 ,尤其是類概述 - >超額空間分布 您可以按照自己的方式找到有關如何使用GridLayout信息。


小費:

不要忘記要使用新的GridLayout ,您需要將其添加為支持庫,並在xml使用:

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

代替

<GridLayout ... > 

我發現這個答案很有幫助。 另外,paulina_glab的答案是使用<android.support.v7.widget.GridLayout而不僅僅是<GridLayout - 謝謝!

特別是每個單元格上的這些屬性:

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

希望能幫助到你。

您可以將edittext限制為單行。

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

絕對奇怪。 如果將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"

並看看這是否有所作為?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM