我从代码中插入一个表。 这是我的XML代码:
<ScrollView
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fillViewport="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="horizontal|vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TableLayoutInsert"
app:layout_constraintVertical_bias="0.0">
<HorizontalScrollView
android:id="@+id/horizontalView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical">
<TableLayout
android:id="@+id/TableLayoutShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"></TableLayout>
</HorizontalScrollView>
</ScrollView>
这是我的代码形式MainActivity:
t1 = (TableLayout) findViewById(R.id.TableLayoutShow);
t1.setColumnStretchable(1, true);
t1.setColumnStretchable(2, true);
ViewAll();
这是ViewAll代码:
Cursor res = myDb.getAllData();
while(res.moveToNext())
{
tr = new TableRow(this);
tr.setBackgroundColor(Color.RED);
src1 = new TextView(this);
src1.setText(res.getString(0));
src1.setTextSize(15);
src1.setTextColor(Color.BLACK);
src1.setGravity(Gravity.CENTER_HORIZONTAL);
src1.setWidth(50);
tr.addView(src1);
src2 = new TextView(this);
src2.setText(res.getString(1));
src2.setTextColor(Color.BLACK);
src2.setTextSize(15);
src2.setGravity(Gravity.LEFT);
tr.addView(src2);
src2.setBackgroundColor(Color.BLUE);
src3 = new TextView(this);
src3.setText(res.getString(2));
src3.setTextSize(15);
src3.setTextColor(Color.BLACK);
src3.setGravity(Gravity.LEFT);
tr.addView(src3);
t1.addView(tr);
}
请帮助我,它显示但是,第2列和第3列是包装内容,不可拉伸。谢谢。