2014-03-06 68 views
0

我已经浏览了所有问过同样事情的人,而且没有人回答过这个问题。出于某种原因,我似乎无法找到如何做到这一点。 我需要在每个动态生成的TableRows之间添加一个边距。 这就是我现在拥有的。 Android保证金表格行

正如您所看到的,这些行是在一起的,使得GUI不可用于用户。 TableLayout在ScrollView中。 这是我用来动态创建TextView和按钮的代码。我一直在使用的利润率尝试,但至今没有运气

Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    int width = size.x; 
    int height = size.y; 

    TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1); 

    /* Create a new row to be added. */ 
    TableRow tr = new TableRow(this); 
    TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
      TableLayout.LayoutParams.FILL_PARENT, 
      TableLayout.LayoutParams.WRAP_CONTENT); 

    /* Margin. */ 
    int leftMargin = 0; 
    int topMargin = 200; 
    int rightMargin = 0; 
    int bottomMargin = 0; 

    /* Set margins and create new textview. */ 
    tableRowParams.setMargins(leftMargin, topMargin, rightMargin, 
      bottomMargin); 
    tr.setLayoutParams(tableRowParams); 
    TextView encuestasTextView = new TextView(this); 
    encuestasTextView.setText("Esto es una prueba"); 
    encuestasTextView.setLayoutParams(new TableRow.LayoutParams(
      TableRow.LayoutParams.WRAP_CONTENT, 
      TableRow.LayoutParams.WRAP_CONTENT)); 

    tr.addView(encuestasTextView); 

    Button a = new Button(this); 
    a.setText("Abrir encuesta"); 
    a.setLayoutParams(new TableRow.LayoutParams(width/3, 
      TableRow.LayoutParams.WRAP_CONTENT)); 

    tr.addView(a); 

    tl.addView(tr, new TableLayout.LayoutParams(
      TableLayout.LayoutParams.FILL_PARENT, 
      TableLayout.LayoutParams.WRAP_CONTENT)); 

这是XML

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".Encuestas" > 

<ScrollView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_below="@+id/button1" 
android:layout_weight="1" 
android:scrollbars="vertical" > 

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:stretchColumns="*" 
    android:id="@+id/TableLayout1"> 


</TableLayout> 


</ScrollView> 
    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="14dp" 
    android:layout_marginTop="14dp" 
    android:onClick="SyncServer" 
    android:text="Sync" /> 
    </RelativeLayout> 

我缺少什么?

回答

1

我想这:PARAMS您先前设置

tl.addView(tr, new TableLayout.LayoutParams(
     TableLayout.LayoutParams.FILL_PARENT, 
     TableLayout.LayoutParams.WRAP_CONTENT)); 

被消灭的布局。只需使用tl.addView(tr);

+0

令人惊叹!从不改变一切!现在工作 –