我坚持以下几点:
尝试在表格的每一行的 ToggleButton 左侧放置一个左对齐的多行 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 )
我在这里想念什么?
谢谢!