在TableLayout的列之间添加空格

Nic*_*ico 13 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的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" />
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

man*_*mal 14

在TableLayout的情况下,按钮本身就是列.这意味着你必须建议按钮在两者之间保留一些空间.您可以使用布局参数来完成此操作.它们更容易在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);
}
Run Code Online (Sandbox Code Playgroud)

如果您以编程方式设置它们,Android中的大多数维度属性都会占用像素 - 因此您应该使用类似我的dpToPixel()方法.请不要在Android中使用像素值!你以后会后悔的.

如果您不希望最右边的按钮具有此边距,只需检查IF并且不要在其上添加LayoutParam.


XML解决方案:

要避免LayoutInflater擦除XML定义的属性,请在膨胀时执行此操作(从加载视图的布局参数中取消):

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

替代方案:像这样使用GridView:Android:简单的GridView,在网格中显示文本


小智 9

为表行组件中的组件添加填充权限

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

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

 </TableRow>
Run Code Online (Sandbox Code Playgroud)