2013-08-01 131 views
10

下面是我的表代码。我的屏幕看起来像这样http://imgur.com/dFP298o但我想让它看起来像这样http://imgur.com/YuYJiJx。我怎样才能在每一行周围和表格布局周围添加边框?如何在TableLayout周围添加边框?

<TableLayout 
    android:id="@+id/table2" 
    android:layout_width="fill_parent" 
    android:layout_below="@+id/test_button_text23" 
    android:layout_marginLeft="45dp" 
    android:layout_marginBottom="25dp" 
    android:layout_marginRight="45dp"  
    android:layout_height="fill_parent" 
    android:stretchColumns="*" > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView 
      android:gravity="left" 
      android:text="Quantity" 
      android:textStyle="bold" /> 

     <TextView 
      android:gravity="center" 
      android:textStyle="bold" 
      android:text="Item" /> 

    </TableRow> 

</TableLayout>  

 

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

    <TextView 
     android:id="@+id/localTime" 
     android:textColor="#000000" 
     android:gravity="left" /> 

    <TextView 
     android:id="@+id/apprentTemp" 
     android:textColor="#000000" 
     android:gravity="center" /> 

</TableRow> 

 

View row = getLayoutInflater().inflate(R.layout.rows, null); 
((TextView) row.findViewById(R.id.localTime)).setText(item.getString("Item")); 
((TextView) row.findViewById(R.id.apprentTemp)).setText(item.getString("Quantity")); 
+1

看看其他答案。 http://stackoverflow.com/a/7379990/2324810 – Hadriel

+0

它的如此混乱只是编辑我的代码请 – user2589245

回答

20

为了创建一个在你的表行,围着桌子布局的边界,你需要创建一个可绘制作为边界然后将其设置为行的背景。

例如:

RES /绘制/ border.xml

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

RES /布局/ your_layout.xml

<TableLayout 
    android:id="@+id/table2" 
    android:layout_width="fill_parent" 
    android:layout_below="@+id/test_button_text23" 
    android:layout_marginLeft="45dp" 
    android:layout_marginBottom="25dp" 
    android:layout_marginRight="45dp" 
    android:layout_height="fill_parent" 
    android:stretchColumns="*"> 

    <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/border"> 

      <TextView 
       android:gravity="left" 
       android:text="Quantity" 
       android:background="@drawable/border" 
       android:textStyle="bold"/> 

      <TextView 
       android:gravity="center" 
       android:textStyle="bold" 
       android:background="@drawable/border" 
       android:text="Item" /> 

    </TableRow> 

</TableLayout> 

这将不完全一样你发布的图片,但玩它来得到你想要的。

+5

它会匹配的图片好多了,我相信如果你使用 insted –

相关问题