【问题标题】:Adding space between columns of a TableLayout在 TableLayout 的列之间添加空格
【发布时间】:2012-04-02 11:14:42
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

  • 你的意思是,你想在行(而不是列)之间添加空间?
  • 你想在按钮和列名之间有空格还是什么?没有得到确切的想法..
  • @manmal 我每个按钮都有一个新列。我想在列之间添加空格。行之间没关系。
  • 不要忘记将问题标记为已回答 ;)

标签: android divider android-tablelayout


【解决方案1】:

在 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 定义的属性,请在膨胀时执行此操作(取自 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

【讨论】:

  • 感谢 Manmal 它有效,但我完全不明白!当我尝试将 android:layout_marginRight="10dp" 或 android:layout_margin="10dp" 放在我的 button_table_row_category.xml 中时,它不起作用。我怎么能在 XML 中做到这一点?再次感谢!
  • 我实际上没有一个类代表我的布局,因为 MyView 为您的链接上的问题所做的。我只是使用XML,在那种情况下,我该怎么做。我也很惊讶,LayoutInflater 会删除我的 XML 定义属性?为什么 ?那么膨胀 XML 有什么意义呢?
  • 您没有阅读自己的代码吗? ;) 你在那里使用充气机,你只需要添加一个“假”作为第三个参数: TextView button = (TextView) inflater.inflate(R.layout.button_table_row_category, null);
  • 使用XML的重点主要是可读性。 LayoutParams 的处理非常繁琐且非常冗长 - 冗长意味着阅读时会有更多的错误和更多的脑力劳动。
  • 是的,抱歉,在过去的几天里我没睡那么多……但是给充气机添加一个错误的参数仍然不起作用。我迷路了...即使我的按钮无法将文本分成两行,如果它不适合一行...
【解决方案2】:

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

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

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

 </TableRow>

【讨论】:

  • 虽然提问者询问如何以编程方式添加间距,但这有助于我在 XML 中寻找解决方法。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    例如。如果你有 3 列。

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

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

    【讨论】: