如何创建与Android的边框的表格?边框、表格、Android

2023-09-11 12:32:17 作者:澈

我用一个表格布局以显示数据表,但我想与用户定义的列和行与边框的表格。建议?

I use a table layout to display data as table, but I want a table with user-defined columns and rows with borders. Suggestions?

推荐答案

我对这个问题的解决方案是将一个XML绘制资源上的每一个细胞的背景场。通过这种方式,你可以定义你想要的所有单元格边框的形状。唯一不方便的是,极端的单元格边框具有其他宽度的一半,但它没有问题,如果你的表填满整个屏幕。

My solution for this problem is to put an xml drawable resource on the background field of every cell. In this manner you could define a shape with the border you want for all cells. The only inconvenience is that the borders of the extreme cells have half the width of the others but it's no problem if your table fills the entire screen.

一个例子:的

绘制/ cell_shape.xml

drawable/cell_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle"  >
        <solid android:color="#000"/>
        <stroke android:width="1dp"  android:color="#ff9"/>
</shape>

布局/ my_table.xml

layout/my_table.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
            <TableRow android:id="@+id/tabla_cabecera" android:layout_width="match_parent" android:layout_height="match_parent"></TableRow>
            <TableLayout android:id="@+id/tabla_cuerpo" android:layout_height="match_parent" android:layout_width="match_parent">
                <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
                <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView></TableRow>

                <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView  android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
                <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView  android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
            </TableLayout>


</LinearLayout>

编辑:一个例子

An example

EDIT2:另一个例子(与更多的元素:圆形边角,梯度...)

Another example (with more elements: circle corners, gradients...)

我在http://blog.intelligenia.com/2012/02/programacion-movil-en-android.html#more.这是在西班牙,但也有一些codeS和更复杂的表格图片。

I have explained this issue with more details in http://blog.intelligenia.com/2012/02/programacion-movil-en-android.html#more. It's in spanish but there are some codes and images of more complex tables.