2010-10-30 58 views
31

我有一個兩列的TableLayout作爲滾動視圖的唯一子對象。第一列包含TextViews('標籤'),第二列包含EditText/Spinner/DateWidget等('values')。即使我已經爲TableLayout,TableRow &指定了android:layout_width =「fill_parent」(在'values'列中)。Android TableLayout Width

創建活動時,屏幕看起來很完美。但是,如果在EditText中輸入一個非常長的值,則「值」列將超出可見屏幕區域。

我該如何解決這個問題?

感謝

回答

57

您可能希望通過使用權重來定義列的大小。因此,您將定義表格佈局高度以填充父級,但對於每個列,您應將寬度設置爲「0px」,並將權重設置爲您希望列跨度的百分比。因此,假設您希望第一列爲屏幕寬度的30%,您將其權重設置爲「0.3」,將第二列設置爲「0.7」。

試試看看是否有效。

+0

這是終於爲我做的唯一的事情。還要注意,如果你在TableLayout上設置了shrinkColumns或stretchColumns,那麼這會對所有事情產生不利影響。 – 2011-04-08 16:18:48

+0

這個答案對我來說並不清楚,你的意思是在所有TableRow組的所有孩子上設置權重?如果是這樣,使用TableLayout的意義何在?爲什麼不只是使用LinearLayouts?我建議在給你麻煩的列上設置拉伸和收縮。 – satur9nine 2014-01-17 18:45:45

0

試試這個,確保android:singleLine="false"android:inputType="textMultiLine"

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:stretchColumns="1"> 
    <TableRow android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 

     <TextView android:text="Label:" /> 
     <EditText android:id="@+id/entry" android:inputType="textMultiLine" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:singleLine="false" /> 
    </TableRow> 
</TableLayout> 
0

我在Android 2.2也有類似問題的EditText。在我的情況下,添加android:maxWidth =「0sp」屬性幫助。現在EditText字段顯示爲我想要的 - 所以它仍然與其他EditText字段的大小相同,但是,此外,它在輸入長文本時不會調整大小。

這裏是我的EditText定義:

<EditText android:id="@+id/extra_edit" 
     android:padding="3dip" 
     android:inputType="textImeMultiLine" 
     android:maxWidth="0sp" 
     android:layout_width="fill_parent" 
     /> 
+0

EditText中的提示和值變形 - 我只能看到第一個字母。任何解決方法? – 2010-11-17 05:42:36

+0

請注意,即使這是舊的:如果您使用0值,請考慮只使用0px。 sp表示Android會對其應用縮放值,並且0 * anything = 0 – 2012-09-18 13:35:40

7

這個工作對我..

tableLayout.setColumnShrinkable(1,true); 
+1

太棒了!第一個參數是列,第二個設置它是可縮小的!感謝名單! – Paschalis 2013-07-22 00:03:45

5
<TableLayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp"> 
<TableRow 
android:layout_height="fill_parent" 
android:layout_width="fill_parent"> 
<TextView 
android:layout_width="wrap_content" 
android:text="Name " 
android:layout_height="fill_parent"> 
</TextView> 
<EditText 
android:layout_width="0dp" 
android:layout_weight="1" 
android:hint="name" 
android:layout_height="wrap_content" 
> 
</EditText> 
</TableRow> 
</TableLayout> 

此代碼爲我工作對齊的第一列和編輯文本填充父文本視圖。

+0

當您的單元格需要行與行之間的不同大小 – Diesel 2017-09-01 17:24:40

20

解決此問題的實用方法是使用TableLayout中的stretchColumnsshrinkColumns屬性。它們的值應該是基於0的列索引。

舉例來說,如果你有一個兩列的表佈局和:

  • 你想在第二列空的空間(拉伸),因此TableLayout填補適合在大屏幕上,整個父視圖設備
  • 您想第一列的小屏幕設備上收縮(和它的內容可能包/ ellipsized)

您將定義您的TableLayout爲:

<TableLayout 
    android:id="@+id/my_table_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:shrinkColumns="0" > 
</TableLayout> 
+2

時,此方法有很大幫助還要注意,您既可以拉伸也可以縮小列。在我的TextView列上這樣做可以防止它超出屏幕大小,但也可以填充可用空間。 – satur9nine 2014-01-17 18:39:00