【问题标题】:Android: Stretching rows in TableLayout programmaticallyAndroid:以编程方式在 TableLayout 中拉伸行
【发布时间】:2012-03-29 19:02:18
【问题描述】:

我正在尝试动态创建一个 TableLayout,它的所有行都被拉伸,如 in this tutorial 所示。我已经通过 XML 完成了它,但我想从 Activity 中完成它。这是我迄今为止尝试过的代码,但没有成功:

public View getTableWithAllRowsStretchedView() {
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setStretchAllColumns(true);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
    tableLayout.setWeightSum(4);


    for (int i = 0; i < 4; i++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setGravity(Gravity.CENTER);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));

        for (int j = 0; j < 4; j++) {
            Button button = new Button(this);
            final int buttonNumber = (j + i * 4);
            button.setText("" + buttonNumber);
            button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));

            tableRow.addView(button);
        }
        tableLayout.addView(tableRow);
    }

    linearLayout.addView(tableLayout);
    return linearLayout;
}

非常感谢您。

编辑

Screenshot of what I have

what I expect

【问题讨论】:

  • 您可以添加当前和预期的屏幕截图吗?

标签: android android-layout


【解决方案1】:

您的行需要父级的LayoutParams

tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));

【讨论】:

  • @slukain:你的答案是正确的,但只是一个简单的问题:如何知道需要使用哪个 Layout 类?就像你用 TableLayout 替换了 TableRow,为什么?
  • @Waqas 你需要父母的LayoutParams。按钮有TableRow 参数,行有TableLayout 参数,表格应该有LinearLayout 参数(我认为它与TableLayout 一起使用,因为TableLayout.LayoutParams 派生自LinearLayout.LayoutParams , 同样TableLayoutLinearLayout 的扩展。
  • 我还有一个问题:当像我这样的可怜人将错误类型的 LayoutParams 推送到视图时,为什么该死的 android 甚至没有给出任何警告或异常?
  • @SargeBorsch 文档中缺少关于LayoutParams 使用的内容,但至少应该是你只做一次的错误。
【解决方案2】:

Luksprog的回答是正确的。

对于水平拉伸,这样做:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));

对于垂直拉伸,这样做:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));

对于在宽度和高度布局中进行拉伸,请执行以下操作:

TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.FILL_PARENT, 1.0f));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多