在 android 中调整动态 TableLayout 的大小

Resizing dynamic TableLayout in android

我在 Android 中创建了一个动态 TableLayout。问题是,列中存在不必要的间隙。我想删除它,我已经更改了几次代码,但结果令人惊讶。这是我目前能得到的最好的。

TableLayout Image

如您所见,header 和包含 2+ 行的行中也存在间隙。 我想让第一列(即项目)具有最长的宽度,而另一列只是用 header.

包装内容

这里是 XML

<TableLayout
                    android:id="@+id/tableDescription"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:stretchColumns="*">

                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:weightSum="4">

                        <TextView
                            android:layout_width="@dimen/size0"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            android:padding="@dimen/size5"
                            android:text="@string/items" />

                        <TextView
                            android:layout_width="@dimen/size0"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            android:padding="@dimen/size5"
                            android:text="@string/quantity" />

                        <TextView
                            android:layout_width="@dimen/size0"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            android:padding="@dimen/size5"
                            android:text="@string/unitPrice" />

                        <TextView
                            android:layout_width="@dimen/size0"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            android:padding="@dimen/size5"
                            android:text="@string/total" />
                    </TableRow>

                </TableLayout>

这里是制作动态行的代码

private void init() {

    // Array for item
    String[] valuesItem = new String[] {
            "Room asd",
            "Room qwewq",
            "Room kadskadskl",
            "Room oeqwoeqwo",
            "Room uuawuwauaw" };

    final ArrayList<String> listItem = new ArrayList<String>();
    for (int i = 0; i < valuesItem.length; ++i) {
        listItem.add(valuesItem[i]);
    }

    // Array for quantity
    String[] valuesQuantity = new String[] {
            "1",
            "1",
            "2",
            "1",
            "2" };

    final ArrayList<String> listQuantity = new ArrayList<String>();
    for (int i = 0; i < valuesQuantity.length; ++i) {
        listQuantity.add(valuesQuantity[i]);
    }

    // Array for price
    String[] valuesPrice = new String[] {
            "10",
            "10",
            "20",
            "50",
            "30" };

    final ArrayList<String> listPrice = new ArrayList<String>();
    for (int i = 0; i < valuesPrice.length; ++i) {
        listPrice.add(valuesPrice[i]);
    }

    // Array for total
    String[] valuesTotal = new String[] {
            "10",
            "10",
            "40",
            "50",
            "60" };

    final ArrayList<String> listTotal = new ArrayList<String>();
    for (int i = 0; i < valuesTotal.length; ++i) {
        listTotal.add(valuesTotal[i]);
    }

    for (int i = 0; i <valuesItem.length; i++) {

        TableRow row= new TableRow(getActivity());
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
        row.setLayoutParams(lp);
        TextView item = new TextView(getActivity());
        TextView quantity = new TextView(getActivity());
        TextView price = new TextView(getActivity());
        TextView total = new TextView(getActivity());
        item.setText(valuesItem[i]);
        quantity.setText(valuesQuantity[i]);
        price.setText(valuesPrice[i]);
        total.setText(valuesTotal[i]);
        item.setPadding(getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                (getActivity().getResources().getDimensionPixelSize(R.dimen.size5)),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5));
        quantity.setPadding(getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5));
        price.setPadding(getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5));
        total.setPadding(getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5),
                getActivity().getResources().getDimensionPixelSize(R.dimen.size5));
        item.setWidth(getActivity().getResources().getDimensionPixelSize(R.dimen.size0));
        quantity.setWidth(getActivity().getResources().getDimensionPixelSize(R.dimen.size0));
        price.setWidth(getActivity().getResources().getDimensionPixelSize(R.dimen.size0));
        total.setWidth(getActivity().getResources().getDimensionPixelSize(R.dimen.size0));
        item.setBackgroundResource(R.drawable.cell_shape);
        quantity.setBackgroundResource(R.drawable.cell_shape);
        price.setBackgroundResource(R.drawable.cell_shape);
        total.setBackgroundResource(R.drawable.cell_shape);
        row.addView(item);
        row.addView(quantity);
        row.addView(price);
        row.addView(total);
        tableDynamic.addView(row);
    }

}

抱歉,如果有点乱。 谢谢!

通过为每个 TextView 配置 LayoutParams 解决

lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 5f);
        item.setLayoutParams(lp);
        lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 2f);
        quantity.setLayoutParams(lp);
        lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.5f);
        price.setLayoutParams(lp);
        lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.5f);
        total.setLayoutParams(lp);