加入我卡具有以下:安卓:在的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)不換行
什麼時我錯過了嗎?
謝謝!
這似乎與其他答案具有相同的效果。但是文字仍然沒有對齊,請參閱截圖:http://s24.postimg.org/o1kri98ad/after2.png。 – user2403534