2016-03-04 40 views
2

我想在android中創建一個帶有邊框但不能實現任務的表格。如何創建一個完全按照我們在帶有邊框的表格創建的表格?

enter image description here

我與目標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

什麼問題? –

+0

問題是我無法在表格中顯示邊框,否則表格已正確創建。 –

+0

使用表talyout –

回答

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> 
1

創建兩個xml。一個用於行&表,另一個用於列。

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> 
+0

不工作...... :( –

相關問題