首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中创建带边框的表格?

如何在Android中创建带边框的表格?
EN

Stack Overflow用户
提问于 2010-01-21 18:35:26
回答 13查看 374.4K关注 0票数 198

我使用表格布局将数据显示为表格,但我想要一个具有用户定义的列和带边框的行的表格。有什么建议吗?

EN

回答 13

Stack Overflow用户

发布于 2010-10-29 03:27:21

我不得不同意布拉德的观点。这是一个糟糕的回答。Android文档指出,TableLayout容器不会显示边框线,所以将它们发送到Android站点对它们没有任何帮助。我在droidnova上找到了一个“脏”的解决方案,其中包括为TableLayout设置背景色,然后为TableRow设置不同的背景色,并将layout_margin添加到行中。我不喜欢这个解决方案,但它确实适用于行边框。我猜你可以对组成每个“单元格”项的项做同样的事情,但我还没有验证过。

类似于DroidNova上的示例:

代码语言:javascript
复制
<TableLayout android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
  <TableRow android:background="#FFFFFF"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_margin="1dp">
     ...
  </TableRow>
</TableLayout>
票数 56
EN

Stack Overflow用户

发布于 2011-08-19 22:39:58

您也可以通过编程来完成此操作,而不是通过xml,但它有点"hackish“。但是如果一个人没有选择,你就没有选择的余地。代码如下:

代码语言:javascript
复制
TableLayout table = new TableLayout(this);
TableRow tr = new TableRow(this);
tr.setBackgroundColor(Color.BLACK);
tr.setPadding(0, 0, 0, 2); //Border between rows

TableRow.LayoutParams llp = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
llp.setMargins(0, 0, 2, 0);//2px right-margin

//New Cell
LinearLayout cell = new LinearLayout(this);
cell.setBackgroundColor(Color.WHITE);
cell.setLayoutParams(llp);//2px border on the right for the cell


TextView tv = new TextView(this);
tv.setText("Some Text");
tv.setPadding(0, 0, 4, 3);

cell.addView(tv);
tr.addView(cell);
//add as many cells you want to a row, using the same approach

table.addView(tr);
票数 25
EN

Stack Overflow用户

发布于 2013-07-04 02:59:43

要在不编写java代码和使用<shape...>标记创建另一个xml布局的情况下使1dp折叠到每个单元格的边框,您可以尝试以下解决方案:

<TableLayout...>中添加android:background="#CCC"android:paddingTop="1dp"android:stretchColumns="0"

<TableRow...>中添加android:background="#CCC"android:paddingBottom="1dp"android:paddingRight="1dp"

在TableRow中的每个单元格/子级(即<TextView...> )中添加android:background="#FFF"android:layout_marginLeft="1dp"

遵循所述的填充和页边距是非常重要的。这个解决方案将在(X)HTML/CSS中绘制一个1dp边框,也就是边框折叠属性。

<TableLayout...><TableRow...>中的背景颜色表示边框线颜色,<TextView...>中的背景填充表格单元格。如果需要,您可以在单元格中添加一些填充。

下面是一个示例:

代码语言:javascript
复制
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#CCC"
    android:paddingTop="1dp"
    android:stretchColumns="0"
    android:id="@+id/tlTable01">

    <TableRow
        android:background="#CCC"
        android:paddingBottom="1dp"
        android:paddingRight="1dp">
        <TextView 
            android:layout_marginLeft="1dp"
            android:padding="5dp"
            android:background="#FFF"
            android:text="Item1"/>
        <TextView 
            android:layout_marginLeft="1dp"
            android:padding="5dp"
            android:background="#FFF"
            android:gravity="right"
            android:text="123456"/>
    </TableRow>
    <TableRow
        android:background="#CCC"
        android:paddingBottom="1dp"
        android:paddingRight="1dp">
        <TextView 
            android:layout_marginLeft="1dp"
            android:padding="5dp"
            android:background="#FFF"
            android:text="Item2"/>
        <TextView 
            android:layout_marginLeft="1dp"
            android:padding="5dp"
            android:background="#FFF"
            android:gravity="right"
            android:text="456789"/>
    </TableRow>
</TableLayout>
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2108456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档