Android table 布局在 java 中添加跨越 table 宽度的单列行

Android table layout adding single column row that spans table width in java

我正在尝试将单个文本视图行添加到 android 中的 table 该行应合并并居中 table

的宽度

我可以使用 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.Create xml 中的空 TableLayout,如下所示:

<?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>

2.Get 来自 xml 的 TableLayout 引用并创建两个 TableRow,每个都有 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, android.R.color.black,"Desc"));
tableRow1.addView(getTextView(android.R.color.holo_green_dark, android.R.color.black,"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);

使用以下辅助函数以编程方式在 TableRow 中创建每个 TextView。这里的关键点是在 TextView TableRow.LayoutParams 上使用 layoutParams.weight = 1 以能够在 TableRow 中的所有列之间获得相等的宽度:

private TextView getTextView(int backgroundColorId, int textColorId, String text){

    TextView tv = new TextView(this);
    tv.setBackgroundColor(ContextCompat.getColor(this, backgroundColorId));
    tv.setTextColor(ContextCompat.getColor(this, textColorId));
    tv.setText(text);
    tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
    int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
    tv.setPaddingRelative(padding, padding, padding, padding);

    TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
    layoutParams.weight = 1; //this is mandatory to get equal width between all columns in TableRow
    tv.setLayoutParams(layoutParams);

    return tv;
}

结果: