Android:以编程方式添加的 TableLayout 中的 View 未显示

标签 android android-layout android-tablelayout

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

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

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 方面还很陌生,所以我还在学习,但这让我很困惑,我似乎找不到问题所在。

最佳答案

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

关于Android:以编程方式添加的 TableLayout 中的 View 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794345/

相关文章:

Android 线性渐变绘制

android - LinearLayout 与 ScrollView 中的 Layout_weight

android - 如何根据大小在表格行中显示图像

android - 我如何在运行时更改改造 URL

安卓 : drop down a list of items without a spinner

android - LifecycleScope 和 SharedFlow 的组合是否消除了对 ViewModel 的需求?

android - 我如何将单元格高度设置为它在 android TableLayout 中的宽度?

java - Android表格布局从手机边缘开始

java - 有哪些适用于 Android 的 websocket 客户端?

java - 尝试获取字符串是否有作者、标题、出版商等