13

我有一个 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

4 回答 4

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

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

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


XML 中的解决方案:

为避免 LayoutInflater 擦除 XML 定义的属性,请在充气时执行此操作(取自已加载视图的布局参数被忽略):

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

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

于 2012-04-03T10:47:10.527 回答
9

为表格行组件中的组件添加 Padding Right

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

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

 </TableRow>
于 2013-03-15T10:10:00.477 回答
3

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

于 2015-02-06T12:17:39.017 回答
0

尝试setColumnStretchable使用TableLayout. 给它一个列索引并将它的可拉伸属性设置为 true。

例如。如果你有 3 列。

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

以上将为您的所有 3 列之间提供相等的间距tableLayout

于 2014-08-19T12:11:45.773 回答