如何创建一个 table 与我们在 html 中创建的完全一样,即 table 有边框?

How to create a table exactly as we create in html that is table with borders?

我正在尝试在 android 中创建带边框的 table,但无法完成任务。

我正在使用目标 SDK 版本 23。

以下是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Table 2"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="A"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="B"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="C"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="D"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="E"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="F"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="G"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="H"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="I"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="J"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="K"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="L"
            android:textColor="#000000" />
    </TableRow>
</TableLayout>
</RelativeLayout>

问题是我无法在 table 中显示边框,否则会正确创建 table。
请指导我完成图中所示的任务

创建两个 xml。一个用于行 & table,另一个用于列。

table_row.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#000000"/>
</shape>

table.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="2dp" android:color="#515151"/>
    <corners android:radius="3dp" />
</shape>

table.xml 作为背景分配给 TableLayoutTableRow 标签。并将 table_row.xml 指定为 TextView 标签的背景。

示例:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        android:background="@drawable/table">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/table"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="@drawable/table_row"
            android:gravity="center"
            android:text="Table 2"

            android:textColor="#000000" />
    </TableRow>
</TableLayout>

经过大量的搜索和挖掘,我让它工作了。谢谢大家的支持!!!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/enquiryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    android:layout_centerInParent="true"
    android:background="#000000"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_span="4"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Table 2"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="A"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="B"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="C"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="D"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="E"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="F"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="G"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="H"
            android:textColor="#000000" />
    </TableRow>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="I"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="J"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="K"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0.5dp"
            android:background="#ffffff"
            android:gravity="center"
            android:text="L"
            android:textColor="#000000" />
    </TableRow>
 </TableLayout>


</RelativeLayout>