本文介绍了如何以编程方式创建一个全屏幕TableLayout(表格高度坍塌unintendedly)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的 TableLayout 精确匹配我的要求(它填补了其母公司和列拉伸均匀):

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:

什么是错在这里?想到我的code正在做的一样我的XML。我搜索并尝试了很多,但没有得到正确的想法。

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.

推荐答案

试试这个..

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

或在您的 rowParams 更改的TableRow 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);
                }

这篇关于如何以编程方式创建一个全屏幕TableLayout(表格高度坍塌unintendedly)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:08