Android中的动态TableLayout刷新

发布于 2024-12-31 23:59:54 字数 2064 浏览 1 评论 0原文

大家好! 我有这个 TableLayout,创建后屏幕是空的。我没有 xml 文件,因为行数和列数是手动给出的规范。首先,我必须用空的 1 x 1 不可见 TextView-s 填充表格,因为如果我想在坐标 (k, l) 处添加按钮,其中 k <= 行且 l <=,则必须使用此解决方案列。 代码如下:

package hu.harge;

import java.awt.Button;

import javax.swing.text.TableView.TableRow;

import sun.jkernel.Bundle;

public class AccessibleLayoutActivity extends Activity {

    private int rows, columns;
    private TableLayout tl = null;
    private ScrollView sv;

    public void createView(int rows, int columns) {
        // TODO Auto-generated method stub

        this.rows = rows;
        this.columns = columns;
        this.sv = new ScrollView(this);

        tl = new TableLayout(this);
        for (int i = 0; i < rows; i++) {
            TableRow tr = new TableRow(this);
            for (int j = 0; j < columns; j++) {
                TextView tv = new TextView(this);
                tr.addView(tv);
                tv.setMaxHeight(1);
                tv.setMaxWidth(1);

            }
            tl.addView(tr);

        }

        sv.addView(tl, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        this.setContentView(sv);
    }

    public void putButton(int row, int column, final String label) {
        if (tl != null) {

            // TODO Auto-generated method stub
            if (row <= rows && column <= columns) {

                Button b = new Button(this);
                b.setText(label);
                TableRow tr = (TableRow) tl.getChildAt(row - 1);
                tr.removeViewAt(column - 1);
                tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            }
        }

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        createView(6, 3);

        putButton(1, 3, "Exit");
        putButton(2, 1, "Menu");

    }
}

我认为 ListView 不能解决我的问题。

问题是屏幕上没有任何东西发光。

感谢您的回答: 杰里

Hy all!
I have this TableLayout and the screen is empty after creating. I have no xml file because of the specification that the number of rows and columns is given manual. First I have to fill the table with empty 1 x 1 not visible TextView-s, because this solution has to be used if I want to add a button at cordinates (k, l) where k <=rows and l <=columns.
The code is the following:

package hu.harge;

import java.awt.Button;

import javax.swing.text.TableView.TableRow;

import sun.jkernel.Bundle;

public class AccessibleLayoutActivity extends Activity {

    private int rows, columns;
    private TableLayout tl = null;
    private ScrollView sv;

    public void createView(int rows, int columns) {
        // TODO Auto-generated method stub

        this.rows = rows;
        this.columns = columns;
        this.sv = new ScrollView(this);

        tl = new TableLayout(this);
        for (int i = 0; i < rows; i++) {
            TableRow tr = new TableRow(this);
            for (int j = 0; j < columns; j++) {
                TextView tv = new TextView(this);
                tr.addView(tv);
                tv.setMaxHeight(1);
                tv.setMaxWidth(1);

            }
            tl.addView(tr);

        }

        sv.addView(tl, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        this.setContentView(sv);
    }

    public void putButton(int row, int column, final String label) {
        if (tl != null) {

            // TODO Auto-generated method stub
            if (row <= rows && column <= columns) {

                Button b = new Button(this);
                b.setText(label);
                TableRow tr = (TableRow) tl.getChildAt(row - 1);
                tr.removeViewAt(column - 1);
                tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            }
        }

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        createView(6, 3);

        putButton(1, 3, "Exit");
        putButton(2, 1, "Menu");

    }
}

ListView can not be a solution for my problem I think.

The problem is that nothing's shining on the screen.

thanks for the answers:
Geri

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2025-01-07 23:59:55

医生说:
TableRow 的子级不需要在 XML 文件中指定layout_width 和layout_height 属性。 TableRow 始终强制这些值分别为 MATCH_PARENT 和 WRAP_CONTENT。

Doc says:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

柏拉图鍀咏恒 2025-01-07 23:59:54

我在这里看到很多错误:
首先,

tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

应该更改为

tr.addView(b, column - 1, new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

根据元素父级的类,layoutparams 的类应该始终具有这样的前缀。

此外,我没有看到您在哪里为 TableRow 或表格或任何电视设置任何布局。任何视图都应该设置高度和宽度。总是。

如果它没有帮助(您也可能会遇到其他错误),最好开始使用 XML 中的布局,让它们工作,然后将它们一一移至代码中。并检查每一步的功能。还要为所有元素提供一些背景以方便查看它们。并让它们的调试时间更长一点 - 再次,为了更好地看到它们......作为红帽中的狼:-)

I see here many errors:
first,

tr.addView(b, column - 1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

should be changed to

tr.addView(b, column - 1, new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

The class of layoutparams should always have such prefix, according to the class of the element's parent.

Further, I don't see where you set any layouts for TableRow's or the table, or any of tv. Any View should have set height and width. Always.

If it won't help (you can have other errors, too), better start using layouts from XMLs, make them work and later move them to code one after one. And check the functionality on every step. Also give all elements some background to see them. And makt them for debugging time a bit larger - again, to see them better... as a wolf in Red Hat :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文