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

时间:2011-07-12 07:31:31

标签: java android android-layout tablelayout

我想在表格布局中显示数据。数据的数量是动态的,因此我需要在表中实现动态行。为此,我想在一行中显示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));
                }
            }

这是我的输出:

enter image description here

需要

我需要列之间的空格。我该怎么办?

2 个答案:

答案 0 :(得分:5)

android:paddingLeft="5dp"

等每个文本视图小部件设置填充左侧5dp

答案 1 :(得分:2)

您还必须为列添加填充:

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));
相关问题