How to create a full screen TableLayout programmatically (table height collapses unintendedly)

亡梦爱人 提交于 2019-12-07 17:31:24

问题


The following TableLayout matches exactly my requirements (it fills its parent and columns are stretched evenly):

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

However, as I want to have the number of rows being configurable I started to create my layout programmatically:

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;
}

Up to a certain point this works not bad as well. Only issue I have is, that my table collapses vertically:

What is wrong here? Thought my code is doing the same as my XML. I searched and tried quite a lot but didn't get the right idea.


回答1:


Try this..

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);
                }

OR In your rowParams change TableRow to TableLayout and try.

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);
                }


来源:https://stackoverflow.com/questions/20991957/how-to-create-a-full-screen-tablelayout-programmatically-table-height-collapses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!