首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android TableLayout 編輯框顯示異常:stretchColumns 解決列寬問題

     編輯:程式設計綜合問答

問題

TableRow 中 EditText 輸入文字不顯示,疑似列寬為 0。

原因

TableLayout 默認不自動分配列寬,第二列(EditText 所在列)需聲明可伸展。

修正 XML

<TableLayout
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1">  <!-- 索引 1 為第二列 -->

    <TableRow>
        <TextView android:id="@+id/username_label" ... android:text="Username" />
        <EditText android:id="@+id/entry1" android:layout_width="match_parent" ... />
    </TableRow>

    <TableRow>
        <TextView android:id="@+id/password_label" ... android:text="Password" />
        <EditText android:id="@+id/entry2" android:layout_width="match_parent" android:password="true" />
    </TableRow>

</TableLayout>

說明

  • stretchColumns="1" 讓第二列佔據剩餘空間,EditText 獲得足寬顯示輸入內容。
  • 可指定多列:stretchColumns="0,1" 或 stretchColumns="*"(所有列)。
  • shrinkColumns 對應收縮列,防止內容過長撐破佈局。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved