2011-04-20 52 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