在TableLayout的列之间添加空格

时间:2012-04-02 11:14:43

标签: android divider android-tablelayout

我有一个TableLayout,我动态添加TableRows。在每个TableRow中,我添加一个Button。

我只是想在我的列之间添加一些空格(这是我的按钮),但我无法弄清楚如何... 我试图改变所有可能的边距但它不起作用:( 所以也许我在我的代码中犯了一个错误,我将它们从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>

感谢您的帮助。

4 个答案:

答案 0 :(得分:14)

对于TableLayout,Buttons本身就是列。这意味着你必须建议按钮在两者之间保留一些空间。您可以使用布局参数来完成此操作。它们更容易在XML中设置,但它也可以以编程方式工作。您始终使用应用它的元素的父布局的LayoutParam类非常重要 - 在这种情况下,父级是TableRow:

// 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);
}

如果您以编程方式设置它们,Android中的大多数维度属性都会占用像素 - 因此您应该使用类似我的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

答案 1 :(得分:9)

为表格行组件

中的组件添加填充权限
<TableRow
    android:id="@+id/tableRow1">

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

 </TableRow>

答案 2 :(得分:2)

尝试android:layout_marginRight="6dp"这对我有用。

答案 3 :(得分:0)

尝试使用setColumnStretchable的{​​{1}}功能。给它一个columnn索引并将其stretchchable属性设置为true。

EG。如果你有3列。

TableLayout

以上将为TableLayout tblLayout; tblLayout.setColumnStretchable(0, true); tblLayout.setColumnStretchable(1, true); tblLayout.setColumnStretchable(2, true); 的所有3列提供相等的间距。