问题描述
我正在尝试将单个 textview 行添加到 android 中的表中 该行应合并并居中表格的宽度
我可以使用 XML 来实现这一点
<TableLayout>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/red_cell_shape"
android:text="code"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red_cell_shape"
android:padding="5dp"
android:text="desc"
android:textAlignment="center"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red_cell_shape"
android:text="status"
android:textAlignment="center"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/tremptyrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/red_cell_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/no_fg_scanned"
android:padding="5dp"
android:layout_span="6"
android:textAlignment="center"
android:textColor="@color/red" />
</TableRow>
</TableLayout>
但我想在java中动态地做并尝试过但没有用
解决方法
您可以像下面这样以编程方式实现上述布局:
1.在xml中创建一个空的TableLayout,如下所示:
at checkExecSyncError (child_process.js:629:11)
at Object.execSync (child_process.js:666:13)
at Object.installBoxDependencies (/usr/local/lib/node_modules/truffle/build/webpack:/packages/box/dist/lib/utils/unbox.js:136:1)
at Object.setUpBox (/usr/local/lib/node_modules/truffle/build/webpack:/packages/box/dist/lib/utils/index.js:66:1)
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/box/dist/box.js:183:1
at Generator.next (<anonymous>)
at fulfilled (/usr/local/lib/node_modules/truffle/build/webpack:/packages/box/dist/box.js:5:42)
at process._tickCallback (internal/process/next_tick.js:68:7)
2.从 xml 中获取 TableLayout 引用并创建两个 TableRow,每个都有 TextView 子项:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
使用下面的辅助函数以编程方式在 TableRow 中创建每个 TextView。这里的关键点是在 TextView //get the TableLayout
TableLayout tableLayout = findViewById(R.id.tableLayout);
//create the first TableRow
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
tableRow1.setBackgroundColor(ContextCompat.getColor(this,android.R.color.white));
//add 3 TextViews in TableRow1 and add TableRow1 to TableLayout
tableRow1.addView(getTextView(android.R.color.holo_purple,android.R.color.black,"Code"));
tableRow1.addView(getTextView(android.R.color.holo_red_light,"Desc"));
tableRow1.addView(getTextView(android.R.color.holo_green_dark,"Status"));
tableLayout.addView(tableRow1);
//create the second TableRow
TableRow tableRow2 = new TableRow(this);
tableRow2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
tableRow2.setBackgroundColor(ContextCompat.getColor(this,android.R.color.white));
//add 1 TextView in TableRow2 and add TableRow2 to TableLayout
tableRow2.addView(getTextView(android.R.color.black,android.R.color.holo_red_light,"No records"));
tableLayout.addView(tableRow2);
上使用 layoutParams.weight = 1
以便能够在 TableRow 中的所有列之间获得相等的宽度:
TableRow.LayoutParams
结果: