如何在Android中将TableRow宽度设置为最大值?

She*_*d25 3 java xml android tablerow android-tablelayout

我在 .xml 文件中定义了一个表,但必须动态添加行和标题。我在水平和垂直方向的表格行填充宽度有问题。我试过stretchColumns参数,但没有帮助。最好的方法是什么?

            table = (TableLayout) findViewById(R.id.testList);

        TableRow headerRow  = new TableRow(this);

        JSONObject obj;

        obj = new JSONObject(loadJSONFromAsset());

        JSONArray records = obj.getJSONArray("tests");

        ArrayList<String> headers = new ArrayList<>();
        headers.add("Header 1");
        headers.add("Header 2");
        headers.add("Header 3");
        headers.add("Header 4");
        headers.add("Header 5");

        for(int i = 0; i < headers.size(); i++) {
            TextView header = new TextView(this);
            header.setText(headers.get(i));
            header.setTextColor(Color.WHITE);
            header.setBackgroundColor(Color.rgb(24, 116, 205));
            header.setPadding(15, 15, 15, 15);
            header.setTextSize(20);
            header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

            header.setMaxLines(1);
            headerRow.addView(header);

        }

        table.addView(headerRow);
Run Code Online (Sandbox Code Playgroud)

和 xml

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <HorizontalScrollView
            android:layout_height="wrap_content"
            android:layout_width="match_parent">

            <TableLayout
                android:id="@+id/testList"
                android:layout_height="match_parent"
                android:layout_width="wrap_content">

            </TableLayout>
        </HorizontalScrollView>
    </ScrollView>

</LinearLayout>

</GridLayout>
Run Code Online (Sandbox Code Playgroud)

Sre*_*ari 5

试试这个方法。以下将确保TableRow与完整的可用宽度对齐。

在基本组件之后,将你的TableLayout放在 XML 中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLL"
    android:orientation="vertical">
<TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在在您的逻辑中,您可以根据响应中的项目数进行添加。这应该在 for 循环中。

for(int i = 0; i < leftComponent.size(); i++) 
    {
            //Table Layout parameters
            TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
            TableLayout tableLayout = (TableLayout) view.findViewById(R.id.fenceTableLayout);
            TableRow trHead = new TableRow(context);
            LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            trHead.setLayoutParams(tableRowParams);

            TextView nameHead = new TextView(context);
            nameHead.setText(leftComponent[i]);
            nameHead.setLayoutParams(textViewParam);
            nameHead.setGravity(Gravity.CENTER);
            trHead.addView(nameHead);

            TextView detailHead = new TextView(context);
            detailHead.setText(rightComponent[i]);
            detailHead.setGravity(Gravity.CENTER);
            detailHead.setLayoutParams(textViewParam);

            trHead.addView(detailHead);
            tableLayout.addView(trHead);

    }
Run Code Online (Sandbox Code Playgroud)

您可能需要根据您的要求进行定制。