2013-05-21 19 views
0

加入我卡具有以下:安卓:在的EditText的TableRow不換行時碼

試圖將左對齊多的EditText的切換按鈕的左側爲表的每一行。

佈局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="right" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right|center_vertical" > 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#00000000" 
      android:ems="10" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:inputType="none" 
      android:text="A very long line that should be wrapped into two or more lines as it doesn't fit on one line" /> 

     <ToggleButton 
      android:id="@+id/toggleButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="ToggleButton" /> 

    </TableRow> 

</TableLayout> 

</LinearLayout> 

這就產生正是我要找的(例如:http://s8.postimg.org/6ldgsovr9/before.png)。

但後來我嘗試添加一個新行使用此代碼:

TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1); 

    TableRow row = new TableRow(this); 
    row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)); 
    row.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 

    EditText et = new EditText(this); 
    et.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT)); 

    TableRow.LayoutParams params = (TableRow.LayoutParams) et.getLayoutParams(); 
    params.height = TableRow.LayoutParams.WRAP_CONTENT; 
    et.setLayoutParams(params); 

    et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
    et.setBackgroundColor(Color.TRANSPARENT); 
    et.setText("A very long line that should be wrapped into two ore more lines as it doesn't fit on one line"); 

    row.addView(et); 

    ToggleButton tb = new ToggleButton (this); 
    row.addView(tb); 

    tl.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT)); 

這打亂了既有線和新線都(例如:http://s24.postimg.org/3ty1ne71x/after.png)不換行

什麼時我錯過了嗎?

謝謝!

回答

0

嘗試評論以下行。 //et.setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);

+0

這似乎與其他答案具有相同的效果。但是文字仍然沒有對齊,請參閱截圖:http://s24.postimg.org/o1kri98ad/after2.png。 – user2403534

0

希望我能幫到你:D

如果你想設置Multline on XML。在TextView之間添加這個標籤:

android:maxLines="4" 
android:lines="4" 

如果您想在運行時設置它。用這個。

mTextView.setMaxLines(maxlines) 
mTextView.setLines(lines) 
+0

謝謝,這使EditText控件多行,但文本尚未左對齊,如在普通的XML版本。文本被切斷,如下所示:http://s24.postimg.org/o1kri98ad/after2.png。 – user2403534

+0

@ user2403534你是否設置了android:gravity? –

+0

我已經添加了'et.setGravity(Gravity.LEFT);'但它看起來是一樣的 – user2403534

0

它看起來像下面這行解決了顯示問題:

et.setEms(10); 

我不知道爲什麼會這樣有差別(字體大小保持不變),但現在的文本正確對齊。

1

我面臨着類似的問題......雖然其後期的答覆,只是張貼因此它可能幫助別人......

我通過將這些屬性TableLayout

android:stretchColumns="0" 
android:shrinkColumns="0" 

解決問題,也可以設置的inputType對於EditText

android:inputType="textMultiLine" 
+0

我所需要的全部是shrinkColumns(在我的例子中爲「1」)。 –

+0

它的工作。應該是正確的答案。謝謝 – Plugie