2010-06-24 101 views
1

我有一個TableLayout有三列,代表一個表格與TableLayout列大小調整

requiredSymbol |標籤| inputfield。

我想要輸入字段填充屏幕右側的標籤剩餘寬度。到目前爲止,我發現的唯一解決方案是這樣的

<TableLayout android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:stretchColumns="2"> 
    <TableRow> 
        <TextView android:text="@string/requiredSymbol" 
           /> 
    <TextView android:text="@string/Name" 
           android:textColor="@color/orange" 
           android:gravity="left|center_vertical" 
     /> 
    <EditText android:id="@+id/contactName" 
     android:inputType="text|textPersonName" 
           android:ellipsize="end" 
          android:singleLine="true" 
          android:scrollHorizontally="true" 
          android:maxWidth="1sp" 
          /> 
    </TableRow> 

maxWidth需要有一些值,實際上它被忽略。然後stretchColumns值很好地完成它的事情。請注意,如果EditText的內容會使表格超出屏幕邊界(這是一個錯誤..),那麼這也適用。

現在很好用,但對我來說似乎有些破綻。有沒有更好的辦法?

回答

0

似乎在這個階段沒有更好的方法使用靜態XML文件。然而,我已經改變了佈局,沒有爲所需的標籤/圖標設置單獨的列,並將其更改爲在每個字段上使用EditText#setError消息並結合TextWatcher實現,無論如何它都更好。

2

嘗試在列上使用android:layout_weight,但請確保在TableLayout中設置了android:stretchColumns。我曾經有過使用TableLayout單方面決定縮小列造成不必要的文本換行的問題。我不確定是否使用layout_weight和單邊列收縮相關。

0

,我實現了,這是通過獲取EditText領域的width它添加到佈局後寬度FILL_PARENT,然後使用該設置MaxWidth的方式。

它找出來是這樣的:

EditText exampleText = new EditText(this); 
exampleText.setText([Your Text]); 

//This says to fill width, but wrap content for the height 
//So it should expand to whatever you've set the MaxLines to be... 
LayoutParams exampleLP = new LayoutParams(FILL_PARENT, WRAP_CONTENT); 
[Parent].addView(exampleText, exampleLP); 

//Here is where it's actually setting the width. 
exampleText.setMaxWidth(exampleText.getWidth()); 

不是我喜歡的方式,但它絕對可靠。

+0

雖然這可能工作,我不喜歡在代碼解決方案。 – 2011-08-05 16:03:27

0

我能夠通過在FrameLayout中包裝EditText並將EditText設置爲match_parent來獲得EditText以匹配剩餘寬度。這似乎也適用於Spinners。

<TableLayout 
    android:shrinkColumns="1" 
    android:stretchColumns="1" > 

    <TableRow> 

     <!-- column 0 --> 
     <TextView 
      android:text="some text" /> 

     <!-- column 1 --> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

     </FrameLayout> 

    </TableRow> 

</TableLayout> 
+0

這不包含可能會或可能不會考慮在內的所需符號。 – 2012-03-31 15:49:56