Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在表布局中显示动态行的问题:Android_Java_Android_Android Layout_Tablelayout - Fatal编程技术网

Java 在表布局中显示动态行的问题:Android

Java 在表布局中显示动态行的问题:Android,java,android,android-layout,tablelayout,Java,Android,Android Layout,Tablelayout,我想在表格布局中显示数据。数据的数量是动态的,所以我需要在表中实现动态行。为此,我想在一行中显示4个TextView 问题 目前,我可以用四个TextView在表和中显示数据,但我想在所有四个EditText之间留一些空格,但无法这样做 这是我的密码 for(ItemRow row : this.getRows()) { if(row != null) { // delcare a new row

我想在表格布局中显示数据。数据的数量是动态的,所以我需要在表中实现动态行。为此,我想在一行中显示4个TextView

问题

目前,我可以用四个TextView在表和中显示数据,但我想在所有四个EditText之间留一些空格,但无法这样做

这是我的密码

for(ItemRow row : this.getRows()) {

                if(row != null) {
                    // delcare a new row
                    newRow = new TableRow(this);
                    newRow.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    newRow.setPadding(0, 2, 0, 2);

                    column1 = new TextView(this);
                    column2 = new TextView(this);
                    column3 = new TextView(this);
                    column4 = new TextView(this);

                    column1.setTextColor(Color.BLACK);
                    column2.setTextColor(Color.BLACK);
                    column3.setTextColor(Color.BLACK);
                    column4.setTextColor(Color.BLACK);
                    column1.setSingleLine(false);
                    column2.setSingleLine(false);
                    column3.setSingleLine(false);
                    column4.setSingleLine(false);

                    if(row.getColumnOne() != null){
                        column1.setText(row.getColumnOne());
                    } 

                    if(row.getColumnTwo() != null) {
                        column2.setText(row.getColumnTwo());
                    }

                    if(row.getColumnThree() != null) {
                        column3.setText(row.getColumnThree());
                    }

                    if(row.getColumnFour() != null) {
                        column4.setText(row.getColumnFour());
                    }

                    //Now add a row 
                    newRow.addView(column1);
                    newRow.addView(column2);
                    newRow.addView(column3);
                    newRow.addView(column4);
                    //Add row to table
                    table.addView(newRow, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                }
            }
这是我的输出:

需要


我需要在列之间留出空间。如何操作?

您还必须为列添加paddind,如下所示:

column1.setPadding(5, 5, 5, 5);    
column2.setPadding(5, 5, 5, 5);    
column3.setPadding(5, 5, 5, 5);    
column4.setPadding(5, 5, 5, 5);
编辑: 然后可以按如下方式设置layoutparams:

column1.setLayoutParams(new LinearLayout.LayoutParams(60,90));

为每个TextVew小部件(如android:paddingLeft=“5dp”

设置padding left 5dp),但如何在自定义行的文本视图中设置多行?因为textview中的文本非常大,然后行变长,只有一行。然后您可以为textview设置LayoutParams,如column1.setLayoutParams(新的LinearLayout.LayoutParams(60,90));