如何以编程方式创建全屏TableLayout(表格高度意外折叠)

2024-01-04

下列TableLayout完全符合我的要求(它填充其父级并且列均匀拉伸):

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#7434a6" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#836492" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#103456" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#958453" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#756aa9" />
    </TableRow>

</TableLayout>

但是,由于我希望行数可配置,因此我开始以编程方式创建布局:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Context context = getActivity();

    TableLayout.LayoutParams tableParams = 
      new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
                                   TableLayout.LayoutParams.MATCH_PARENT);
    TableRow.LayoutParams rowParams = 
      new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
    TableRow.LayoutParams itemParams = 
      new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
                                TableRow.LayoutParams.MATCH_PARENT, 1f);

    TableLayout tableLayout = new TableLayout(context);
    tableLayout.setLayoutParams(tableParams);
    tableLayout.setBackgroundColor(0xff7434a6);

    for (int row = 0; row < 2; row++) {
        TableRow tableRow = new TableRow(context);
        tableRow.setLayoutParams(rowParams);

        for (int column = 0; column < 2; column++) {
            Random color = new Random();
            int randomColor = 
              Color.argb(255, color.nextInt(256), 
                              color.nextInt(256),
                              color.nextInt(256));

            TextView textView = new TextView(context);
            textView.setLayoutParams(itemParams);
            textView.setBackgroundColor(randomColor);

            tableRow.addView(textView);
        }

        tableLayout.addView(tableRow);
    }

    return tableLayout;
}

在某种程度上,这也很有效。我唯一的问题是,我的桌子垂直折叠:

这里有什么问题吗?我认为我的代码与 XML 的作用相同。我进行了很多搜索和尝试,但没有得到正确的想法。


尝试这个..

LinearLayout.LayoutParams tableParams = 
                  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
                          LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams rowParams = 
                  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
        LinearLayout.LayoutParams itemParams = 
                  new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                          LinearLayout.LayoutParams.MATCH_PARENT, 1f);

                        LinearLayout tableLayout = new LinearLayout(context);
                tableLayout.setLayoutParams(tableParams);
                tableLayout.setOrientation(LinearLayout.VERTICAL);
                tableLayout.setBackgroundColor(0xff7434a6);

                for (int row = 0; row < 2; row++) {
                    LinearLayout tableRow = new LinearLayout(context);
                    tableRow.setOrientation(LinearLayout.HORIZONTAL);
                    tableRow.setLayoutParams(rowParams);

                    for (int column = 0; column < 2; column++) {
                        Random color = new Random();
                        int randomColor = 
                          Color.argb(255, color.nextInt(256), 
                                          color.nextInt(256),
                                          color.nextInt(256));

                        TextView textView = new TextView(context);
                        textView.setLayoutParams(itemParams);
                        textView.setBackgroundColor(randomColor);

                        tableRow.addView(textView);
                    }

                    tableLayout.addView(tableRow);
                }

或者在你的行参数改变TableRow to TableLayout并尝试。

TableLayout.LayoutParams tableParams = 
                  new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
                                               TableLayout.LayoutParams.MATCH_PARENT, 1f);
        TableLayout.LayoutParams rowParams = 
                  new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
                TableRow.LayoutParams itemParams = 
                  new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 
                                            LayoutParams.FILL_PARENT, 1f);

                TableLayout tableLayout = new TableLayout(MainActivity.this);
                tableLayout.setLayoutParams(tableParams);
                tableLayout.setBackgroundColor(0xff7434a6);

                for (int row = 0; row < 2; row++) {
                    TableRow tableRow = new TableRow(MainActivity.this);
                    tableRow.setLayoutParams(rowParams);

                    for (int column = 0; column < 2; column++) {
                        Random color = new Random();
                        int randomColor = 
                          Color.argb(255, color.nextInt(256), 
                                          color.nextInt(256),
                                          color.nextInt(256));

                        TextView textView = new TextView(MainActivity.this);
                        textView.setLayoutParams(itemParams);
                        textView.setBackgroundColor(randomColor);

                        tableRow.addView(textView);
                    }

                    tableLayout.addView(tableRow);
                }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式创建全屏TableLayout(表格高度意外折叠) 的相关文章