Android:TableLayout中的视图以编程方式添加而不显示

时间:2016-03-04 10:59:44

标签: android android-layout android-tablelayout

我正在创建一个应用程序,我需要在其中包含动态表,所以我尝试这样做,并且在大多数情况下,它的工作原理。但是我在行和列之间有这些分隔符,我使用Views这样做,它们就像这样出现:

编辑:我认为视图没有显示,我的第一列是左边框应该是的,不知道这是做什么的。

Dividers appear in the title row

这是我用来生成行的代码:

private void generateMaterialRows(){
        TableLayout tableMaterials = (TableLayout) findViewById(R.id.info_table_materials);
            //create counter for index in table
            int c = 3;
        for(Material m: materials){


            //makes Rows
            TableRow tRow = new TableRow(this);
            TableRow tDivider = new TableRow(this);

            //Makes 5 bottomBorders and puts them in an Array
            View[] bottomBorders = new View[5];

            for (int i = 0; i < 5; i++) {
                View bottomBorder = new View(this);
                bottomBorder.setBackgroundResource(R.color.colorDarkGray);
                int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
                bottomBorder.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
                bottomBorder.setLayoutParams(bottomBorder.getLayoutParams());

                bottomBorders[i] = bottomBorder;
            }

            //makes 3 sideBorders and puts them in an Array
            View[] sideBorders = new View[3];

            for (int i = 0; i < 3; i++){
                View sideBorder = new View(this);
                sideBorder.setBackgroundResource(R.color.colorDarkGray);
                int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
                sideBorder.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
                sideBorder.setLayoutParams(sideBorder.getLayoutParams());

                sideBorders[i] = sideBorder;
            }

            //makes columnAmount
            TextView txtAmount = new TextView(this);
            txtAmount.setBackgroundResource(R.color.colorBlueLight);
            txtAmount.setPadding(5,5,5,5);
            txtAmount.setText(String.valueOf(m.getAmount()));

            //makes columnName
            TextView txtName = new TextView(this);
            txtName.setBackgroundResource(R.color.colorBlueLight);
            txtName.setPadding(5, 5, 5, 5);
            txtName.setText(String.valueOf(m.getName()));

            //puts content in TableRow
            tRow.addView(sideBorders[0]);
            tRow.addView(txtAmount);
            tRow.addView(sideBorders[1]);
            tRow.addView(txtName);
            tRow.addView(sideBorders[2]);

            //makes divider
            for (int i = 0; i < 5; i++){
                tDivider.addView(bottomBorders[i]);
            }

            tableMaterials.addView(tRow, c);
            tableMaterials.addView(tDivider, c+1);

            c++;
        }
    }

这是XML中的TableLayout:

                <TableLayout
                    android:id="@+id/info_table_materials"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/activity_vertical_margin">
                    <!-- Divider Above Title Row -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                    </TableRow>

                    <!-- Title Row -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                        <TextView
                            android:text="@string/info_amount"
                            android:background="@color/colorLightGray"
                            android:paddingRight="10dp"
                            android:paddingLeft="10dp"
                            android:paddingTop="5dp"
                            android:paddingBottom="5dp"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                        <TextView
                            android:text="@string/info_materials"
                            android:background="@color/colorLightGray"
                            android:paddingRight="10dp"
                            android:paddingLeft="10dp"
                            android:paddingTop="5dp"
                            android:paddingBottom="5dp"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_width="3px"
                            android:layout_height="fill_parent"/>
                    </TableRow>

                    <!-- Divider Between Title and Content Rows -->
                    <TableRow>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                        <View
                            android:background="@color/colorDarkGray"
                            android:layout_height="3px"/>
                    </TableRow>

                </TableLayout>

我还是在Android上相当新,所以我还在学习但是这个让我真的很困惑,我似乎无法找到错误。

1 个答案:

答案 0 :(得分:1)

我发现了问题。我不知道如何使用LayoutParams所以我使用了ViewGroup.LayoutParams,但因为它们位于TableRow我应该使用TableRow.LayoutParams