【问题标题】:Centering TableLayout rows using Java Code使用 Java 代码将 TableLayout 行居中
【发布时间】:2013-01-08 06:00:48
【问题描述】:

我会创建一个 TableLayout 并将其内容居中。 我写了这段代码:

TableLayout tl = new TableLayout(this);
tl.setGravity(Gravity.CENTER);

TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);

但它不起作用。行居中但仅垂直...我该怎么做?

提前致谢。

编辑:

TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL));

TableRow firstRow = new TableRow(this);
TableRow secondRow = new TableRow(this);

firstRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));
secondRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, Gravity.CENTER));

【问题讨论】:

    标签: android tablelayout


    【解决方案1】:

    您应该为 TableLayout 设置一个 LayoutParams 并在其上设置重力:

    tl.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL) );
    

    【讨论】:

    • 用你的代码行我的布局内容在显示屏的上角,在左边:)
    • 请将 LayoutParams.WRAP_CONTENT 更改为 LayoutParams.FILL_PARENT
    • TableLayout 中的行需要父级的 LayoutParams。尝试将 LayoutParams 设置为您的行: firstRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, Gravity.CENTER));
    • 我在答案中编辑了代码。使用此代码,我获得:postimage.org/image/upz8slxsx :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多