如何在Android中设置TableRow的宽度



我有一个表定义在.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);
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>

试试这个方法。下面将确保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>

现在在您的逻辑中,您可以根据响应中的项数进行添加。这应该在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);
    }

您可能需要根据您的需求定制

最新更新