2011-04-20 93 views
1

我在我的TableLayout的列中有一個TextView。如果TextView的文本長度大於列可以容納的長度,我希望TextView截斷和橢圓化,但它只是拉伸列並破壞我的佈局。沒有任何關於TextView截斷的建議我找到了,我假設這是由於表。有什麼建議麼?Android:如何截斷TableLayout中的TextView?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <RelativeLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/Icon" 
      android:src="@drawable/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 
     <TextView 
      android:id="@+id/Value" 
      android:layout_toRightOf="@id/Icon" 
      android:text="very long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long stringvery long string" 
      android:inputType="text" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </TextView> 
    </RelativeLayout> 

*編輯:代碼表:

int fiveDip = convToDip(5, getContext()); 

Table table = new TableLayout(getContext()); 
table.setPadding(fiveDip, fiveDip, fiveDip, fiveDip); 
table .setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT)); 
+0

你能提供您TableLayout XML? – Joe 2011-04-20 19:02:11

+0

@Joe。我在代碼中定義它,我將代碼添加爲我的問題的編輯。 – ab11 2011-04-20 19:08:04

回答

0

你的XML和代碼似乎無關,你說的問題。不過,你有沒有嘗試過爲你的TextView做android:singleLine="true"。對於選取框,請參見Android automatic horizontally scrolling TextView

此外,您可以用包裝您的TableLayout以啓用滾動。

+0

我試着只添加singleLine =「true」,然後通過使用您的代碼框選擇。都沒有工作。 A會喜歡截斷,而不是用scrollview包裝。 – ab11 2011-04-20 20:17:59

11

您需要設置上都包含TextView的列shrinkColumnsstretchColumns

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:shrinkColumns="1" 
    android:stretchColumns="1">  
     <TableRow> 
      <View 
       android:background="#f00" 
       android:layout_height="fill_parent" 
       android:layout_width="40dp" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:textSize="18sp" 
       android:textColor="#fff" 
       android:ellipsize="end" 
       android:singleLine="true" 
       android:text="Example text that is very loooooooooooooooooooooooooooooooooooooooooooooong" /> 

      <View 
       android:background="#00f" 
       android:layout_height="fill_parent" 
       android:layout_width="40dp" /> 
     </TableRow> 

</TableLayout> 
+0

這也是我出錯的地方,你必須告訴它哪個列索引應該縮小和增長。 – 2011-10-06 10:00:56