2012-04-02 77 views
12

我有一个TableLayout,我在那里动态添加TableRows。在每个TableRow中,我添加一个Button。在TableLayout的列之间添加空格

我只是想在我的列(这是我的按钮)之间添加一些空间,但我不知道如何... 我试图改变所有可能的利润率,但它不起作用: ( 所以也许我改变了我的代码错误,我夸大他们从XML文件:

private void createButtons(final CategoryBean parentCategory) { 
    final List<CategoryBean> categoryList = parentCategory.getCategoryList(); 
    title.setText(parentCategory.getTitle()); 
    // TODO à revoir 
    int i = 0; 
    TableRow tr = null; 
    Set<TableRow> trList = new LinkedHashSet<TableRow>(); 
    for (final CategoryBean category : categoryList) { 

     TextView button = (TextView) inflater.inflate(R.layout.button_table_row_category, null); 
     button.setText(category.getTitle()); 
     if (i % 2 == 0) { 
      tr = (TableRow) inflater.inflate(R.layout.table_row_category, null); 
      tr.addView(button); 
     } else { 
      tr.addView(button); 
     } 

     trList.add(tr); 

     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       CategoryBean firstChild = category.getCategoryList() != null && !category.getCategoryList().isEmpty() ? category 
         .getCategoryList().get(0) : null; 
       if (firstChild != null && firstChild instanceof QuestionsBean) { 
        Intent intent = new Intent(CategoryActivity.this, QuestionsActivity.class); 
        intent.putExtra(MainActivity.CATEGORY, category); 
        startActivityForResult(intent, VisiteActivity.QUESTION_LIST_RETURN_CODE); 
       } else { 
        Intent intent = new Intent(CategoryActivity.this, CategoryActivity.class); 
        intent.putExtra(MainActivity.CATEGORY, category); 
        startActivityForResult(intent, VisiteActivity.CATEGORY_RETURN_CODE); 
       } 
      } 
     }); 
     i++; 
    } 
    for (TableRow tableRow : trList) { 
     categoryLaout.addView(tableRow); 
    } 
} 

我button_table_row_category.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/buttonTableRowCategory" 
    style="@style/ButtonsTableRowCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/validate" /> 

我table_row_category.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableRowCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="100dp" 
    android:gravity="center" 
    android:padding="5dp" > 

</TableRow> 

感谢您的帮助。

+0

你确实是说,你想在行之间添加空格(不是列?) – manmal 2012-04-02 11:17:38

+0

你想在按钮和列名之间的空间还是什么?没有得到确切的想法.. – Dhruvisha 2012-04-02 11:29:29

+0

@manmal我有一个新的列每个按钮。我想在列之间添加空格。行之间没关系。 – Nico 2012-04-02 11:37:49

回答

13

对于TableLayout,Buttons本身就是列。这意味着你必须建议按钮保持一定的空间。你可以通过使用布局参数来做到这一点。它们在XML中设置要容易得多,但它也可以以编程方式工作。你总是使用LayoutParam类,你运用它的元素的父布局的是非常重要的 - 在这种情况下,父亲是一个的TableRow:在Android的

// Within createButtons(): 
android.widget.TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams(); 
p.rightMargin = DisplayHelper.dpToPixel(10, getContext()); // right-margin = 10dp 
button.setLayoutParams(p); 

// DisplayHelper: 
private static Float scale; 
public static int dpToPixel(int dp, Context context) { 
    if (scale == null) 
     scale = context.getResources().getDisplayMetrics().density; 
    return (int) ((float) dp * scale); 
} 

大多数维度的属性,如果你将它们取像素以编程方式 - 因此你应该使用像我的dpToPixel()方法。请不要在Android中使用像素值!稍后你会后悔的。

如果您不想让最右边的按钮具有此边距,只需检查一个IF并且不要在其上添加LayoutParam。


解决方案在XML:

为了避免LayoutInflater擦掉你的XML定义的属性,这样做,而吹胀(从Layout params of loaded view are ignored拍摄):

View view = inflater.inflate(R.layout.item /* resource id */, 
            MyView.this /* parent */, 
            false /*attachToRoot*/); 

替代:像这样使用GridView:Android: Simple GridView that displays text in the grids

+0

感谢Manmal它的工作原理,但我完全不明白!当我尝试在我的button_table_row_category.xml中放置android:layout_marginRight =“10dp”或android:layout_margin =“10dp”时,它不起作用。我怎么能在XML中做到这一点?再次感谢 ! – Nico 2012-04-03 14:14:18

+0

用XML解决方案更新了答案:) – manmal 2012-04-03 14:34:01

+0

实际上我没有一个代表我的布局的类,因为MyView会为您的链接上的问题提供解决方案。在这种情况下,我只是使用XML,我该怎么做。我也很惊讶,LayoutInflater会擦除我定义的XML属性?为什么 ?那么膨胀XML有什么意义呢? – Nico 2012-04-03 14:51:15

8

表中的行组件

<TableRow 
    android:id="@+id/tableRow1"> 

    <TextView 
     android:id="@+id/textView1" 
     android:paddingRight="20dp" /> 

</TableRow> 
+0

虽然提问者询问如何以编程方式添加间距,但这帮助我寻找在XML中使用一种解决方法。 – 2017-12-26 07:51:49

0

尝试使用TableLayoutsetColumnStretchable功能添加填充权的一个组成部分。给它一个columnn索引并将其可伸展属性设置为true。

例如,如果你有3列。

TableLayout tblLayout; 
tblLayout.setColumnStretchable(0, true); 
tblLayout.setColumnStretchable(1, true); 
tblLayout.setColumnStretchable(2, true); 

以上将给你所有3列tableLayout之间的等距。

1

尝试android:layout_marginRight="6dp"这工作适合我。