在Android中生成对称表

发布于 2024-12-28 17:53:19 字数 608 浏览 1 评论 0原文

我在使用 Android 的 TableLayout 时遇到了一些问题。我想创建一个具有对称行/列的表,即一个布局,其中的每个 TextView 都像其他所有 TextView 一样分配完全相同的空间量,无论附加什么文本。

重要的是,这可以通过使用 Java 代码而不是 XML 文件来完成。

最好知道这种布局需要两个桌子相邻,即每个桌子只有屏幕尺寸的 50%。我想这会使几个LayoutParameter不可用(例如FILL_PARENT)?

这就是我尝试过的:

for (String s : buffer) {
            TextView tv = new TextView(this);
            tv.setLayoutParams(new TableRow.LayoutParams(0x00, LayoutParams.WRAP_CONTENT, 1f));
            tv.append(s);       
            row.addView(tv, new TableRow.LayoutParams(LayoutParams.FILL_PARENT));   
        }   

不过看起来仍然很糟糕。

I got some trouble with the TableLayout Android. I wanna create a table with symmetric rows/columns, i.e. a layout where every TextView in it gets allocated exactly the same amount of space like all the others, no matter what text has been appended to it.

It's important that this can be done by the use of Java-code, not the XML-files.

It would be probably good to know that this layout requires two tables to be next to each other, i.e. every table has just 50% of the screen size. I suppose this makes several LayoutParameter unavailable (e.g. FILL_PARENT)?

This is what I tried:

for (String s : buffer) {
            TextView tv = new TextView(this);
            tv.setLayoutParams(new TableRow.LayoutParams(0x00, LayoutParams.WRAP_CONTENT, 1f));
            tv.append(s);       
            row.addView(tv, new TableRow.LayoutParams(LayoutParams.FILL_PARENT));   
        }   

Still looks bad though.

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

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

发布评论

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

评论(1

哑剧 2025-01-04 17:53:19

这将创建两列,它们以有序的方式共享宽度。

<TableLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <!-- Set the width to 0dp and set layout_weight=1! on both Views-->
        <TextView
            android:text="This is text1, its pretty long but that shouldn't be a problem"
            android:layout_marginLeft="1px"
            android:background="#ff0000"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Shorter"
            android:background="#00ff00"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </TableRow>
</TableLayout>

编辑:同样的事情,但现在在代码中:

    TableLayout layout = new TableLayout(this);
    TextView t1 = new TextView(this);
    t1.setBackgroundColor(Color.BLUE);
    t1.setText("This is text1, its pretty long but that shouldn't be a problem");
    TextView t2 = new TextView(this);
    t2.setBackgroundColor(Color.GRAY);
    t2.setText("Shorter");

    TableRow row = new TableRow(this);
    row.addView(t1, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
    row.addView(t2, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));

    layout.addView(row);

This will create two columns that share's the width in an orderly fashion.

<TableLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <!-- Set the width to 0dp and set layout_weight=1! on both Views-->
        <TextView
            android:text="This is text1, its pretty long but that shouldn't be a problem"
            android:layout_marginLeft="1px"
            android:background="#ff0000"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Shorter"
            android:background="#00ff00"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </TableRow>
</TableLayout>

Edit: Same thing, but now in code:

    TableLayout layout = new TableLayout(this);
    TextView t1 = new TextView(this);
    t1.setBackgroundColor(Color.BLUE);
    t1.setText("This is text1, its pretty long but that shouldn't be a problem");
    TextView t2 = new TextView(this);
    t2.setBackgroundColor(Color.GRAY);
    t2.setText("Shorter");

    TableRow row = new TableRow(this);
    row.addView(t1, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
    row.addView(t2, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));

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