2011-04-26 64 views
3

我嘗試創建一個具有多行的TableLayout,並且每行都有不同的列TableLayout在不同行的不同列

以下顯示列表視圖。對於列表視圖的每一行,它都包含一個只有1行2列的表格佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
    <ImageView 
     android:id="@+id/color_label" 
     android:layout_width="12dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:background="#ffffff" /> 

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1"> 
     <TableRow> 
      <TextView 
       android:id="@+id/toptext" 
       android:gravity="left" 
      /> 
      <TextView 
       android:id="@+id/bottomtext" 
       android:gravity="right" 
      />  
     </TableRow>   

     <View android:layout_height="2dip" 
      android:background="#FF909090" /> 
    </TableLayout> 
</LinearLayout> 

這是結果。

enter image description here

後來,我想進一步提高TableLayout。第一行是2列。其第二排有4列,豎線用3,4列分隔1,2列。

我使用下面的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
    <ImageView 
     android:id="@+id/color_label" 
     android:layout_width="12dip" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:background="#ffffff" /> 

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1"> 
     <TableRow> 
      <TextView 
       android:id="@+id/toptext" 
       android:gravity="left" 
      /> 
      <TextView 
       android:id="@+id/bottomtext" 
       android:gravity="right" 
      />  
     </TableRow>   

     <View android:layout_height="2dip" 
      android:background="#FF909090" /> 

     <TableRow> 
      <TextView 
       android:id="@+id/col1" 
       android:gravity="left" 
       android:text="Column 1" 
      /> 
      <TextView 
       android:id="@+id/col2" 
       android:gravity="right" 
       android:text="Column 2" 
      /> 
      <TextView 
       android:id="@+id/col3" 
       android:gravity="left" 
       android:text="Column 3" 
      /> 
      <TextView 
       android:id="@+id/col4" 
       android:gravity="right" 
       android:text="Column 4" 
      />    
     </TableRow>   


     <View android:layout_height="2dip" 
      android:background="#FF909090" /> 

    </TableLayout> 
</LinearLayout> 

這是結果。

enter image description here

然而,表格佈局的第二行是不是我想要的。我想要的是這樣的。

enter image description here

我怎樣才能提高我的XML來實現上述效果呢?

回答

1

您可以爲不同的項目渲染器創建您自己的可繪製xml(內部爲res/drawable-[h|m|l]dpi),並將它們設置爲背景。

通過這種方式,您可以實現任何類型的邊框,形狀等,並且在您的adaptergetView方法中,您只需使用正確的backgroundDrawable來擴大所需的視圖。

+0

我也有相同的要求但是,如果我有單獨的XML和滾動表水平然後它不能正常移動。我所做的標題是在一個XML中聲明的,而行是在一個XML中聲明的,而且我在這個活動中正在膨脹XML。但是現在發生的事情是它們是兩個,所以當我移動底部視圖時,發生什麼?列不移動。我們不能把listview放在滾動視圖中。 – 2011-10-19 04:40:09