我正在尝试在android中创建一个带边框但无法完成任务的表。
我正在使用目标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>
问题是我无法在表格中显示边框,否则表格会正确创建。
请指导我完成图中所示的任务。
答案 0 :(得分:1)
创建两个xmls。一个用于行&amp;表格和其他列。
<强> 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
作为TableLayout
和TableRow
代码的背景。并指定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>
答案 1 :(得分:0)
经过大量的搜索和挖掘后,我才开始工作。谢谢大家的支持!!!
<?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>