2012-07-10 8 views
11

私はテーブル内の行間に区切り線が必要であるという点で1つのテーブルレイアウトを表示しています。また、テーブルレイアウトで列方向の分離をすることも可能です。テーブルレイアウト内の行間の分離

私のXML表のレイアウトを以下に示します。

<?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="wrap_content" 
    android:orientation="vertical" 
    android:paddingBottom="6dip" 
    android:paddingTop="4dip"> 

    <TableLayout 
     android:id="@+id/tablelayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingRight="2dip"> 

     <TableRow> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Income"></TextView> 

      <TextView 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="150dp" 
       android:text="Expense"></TextView> 
     </TableRow> 

     <TableRow android:layout_marginTop="30px"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Household:"></TextView> 

      <TextView 
       android:id="@+id/text50" 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:text="Household:"></TextView> 
     </TableRow> 


     <TableRow android:layout_marginTop="40px"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_span="2" 
       android:text="Travel:"></TextView> 

      <TextView 
       android:id="@+id/text51" 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-250dp" 
       android:text="Travel"></TextView> 
     </TableRow> 

     <TableRow android:layout_marginTop="40px"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_span="2" 
       android:text="Education:"></TextView> 

      <TextView 
       android:id="@+id/text52" 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="-250dp" 
       android:text="Education"></TextView> 
     </TableRow> 

    </TableLayout> 
</LinearLayout> 
+2

uは表の行2のテキストを追加しているように、一つの画像(行)を追加/幅2dp.thisを有するテキストとの間の分圧器としてビューはまた、これを試みることができる分圧器 – AkashG

+0

として機能します。 http://stackoverflow.com/questions/2108456/how-can-i-create-a-table-with-borders-in-android または http://stackoverflow.com/questions/7117533/how-to-give-border-of-cells in-tablelayout-in-android – spr

答えて

25

これをチェックしてください。それが動作します。

<?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="wrap_content" 
    android:background="@android:color/white" 
    android:orientation="vertical" 
    android:paddingBottom="6dip" 
    android:paddingTop="4dip" > 

    <TableLayout 
     android:id="@+id/tablelayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingRight="2dip" > 

     <TableRow> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Income" > 
      </TextView> 

      <TextView 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="150dp" 
       android:text="Expense" > 
      </TextView> 
     </TableRow> 

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

      <View 
       android:id="@+id/line1" 
       android:layout_width="match_parent" 
       android:layout_height="1dip" 
       android:layout_weight="1" 
       android:background="#FF909090" 
       android:padding="2dip" /> 
     </TableRow> 

     <TableRow android:layout_marginTop="30px" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Household:" > 
      </TextView> 

      <TextView 
       android:id="@+id/text50" 
       android:layout_width="150px" 
       android:layout_height="wrap_content" 
       android:text="Household:" > 
      </TextView> 
     </TableRow> 
     </TableLayout> 

</LinearLayout> 
+0

プログラムで同じことをすることはできますか?私はプログラムベースのレイアウトを作成しています。 – AndroidOptimist

+2

これはそれを行う書き込み方法ではないと思います。あなたは20のテーブルの行を持っている場合、分割線のために20行以上を作成する必要があります.....私は、TableLayoutで "アンドロイド:devider"という名前のタグがあると思うが、私はそれを使用する方法を知っている... ifいくつかのボディはすぐに教えてください... –

+0

アンドロイド:layout_span = "2" 'をディバイダビューに追加することを忘れないでください。 – Roel

4
View v = new View(this); 
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
v.setBackgroundColor(Color.rgb(51, 51, 51)); 
tr.addView(mTvDate); 
tr.addView(mTvResult); 

tl.addView(tr); 
tl.addView(v); 

from here.

+0

ありがとうVinay Wadhwa ..私はこれを追加する必要があります教えてください? –

+0

progrmaticallyを行うのではなく、2つのtextviewの間でxmlのビューを追加してください。 – AkashG

+0

あなたの質問で言及したxmlを使用するアクティビティでこのコードを追加できます。 –

1

私はあなたがTableLayoutの背景とあなたの行の余裕を持ってプレイしていると思う...

+0

これは私がやったやり方でもあり、レイアウト上のビューの膨らみを避けることができるので最適な解決策です。 – olefevre

0

私はあなたのコードにいくつかの変更を加え、これがあなたを助けると思っています...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical">  
<TableLayout 
    android:id="@+id/tablelayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:paddingRight="2dip"> 

    <TableRow > 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Income"> 
    </TextView>    
     <TextView    
      android:layout_width="150px" android:layout_height="wrap_content" android:text="Expense" android:layout_marginLeft="150dp"> 
    </TextView> 
    </TableRow> 
<View android:id="@+id/firstDivider" 
     android:layout_height="2dp" 
     android:layout_width="fill_parent" 
     android:background="#FFFFFF" /> 


    <TableRow android:layout_marginTop="30px"> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Household:" > 
    </TextView>    
     <TextView    
      android:id="@+id/text50" android:layout_width="150px" android:layout_height="wrap_content" android:text="Household:"> 
    </TextView> 
    </TableRow> 
<View android:id="@+id/firstDivider" 
     android:layout_height="2dp" 
     android:layout_width="fill_parent" 
     android:background="#FFFFFF" /> 



<TableRow android:layout_marginTop="40px"> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Travel:" android:layout_span="2"> 
    </TextView>  
     <TextView 
      android:id="@+id/text51" 
      android:layout_width="150px" android:layout_height="wrap_content" android:text="Travel" android:layout_marginLeft="-250dp"> 
    </TextView> 


</TableRow> 


<View android:id="@+id/firstDivider" 
     android:layout_height="2dp" 
     android:layout_width="fill_parent" 
     android:background="#FFFFFF" /> 

    <TableRow android:layout_marginTop="40px"> 
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_span="2"> 
    </TextView>  
     <TextView 
      android:id="@+id/text52" 
      android:layout_width="150px" android:layout_height="wrap_content" android:text="Education" android:layout_marginLeft="-250dp"> 
    </TextView> 

</TableRow> 

<View android:id="@+id/firstDivider" 
     android:layout_height="2dp" 
     android:layout_width="fill_parent" 
     android:background="#FFFFFF" /> 


</TableLayout> 
</LinearLayout> 

ありがとう....

+0

ありがとうuser4232 ...しかし、何も来ていません。テーブルのレイアウトが崩壊する.. –

3

TableLayoutLinearLayoutを拡張しますので、XMLで仕切りを追加することができます。私は、これは当時仕事と想定しています

<TableLayout 
     android:id="@+id/basket_summary_table" 
     android:showDividers="middle" 
     android:divider="@drawable/divider_list" 
     android:padding="@dimen/element_large_spacing" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0"> 
    </TableLayout> 

私の分周器は、次のようになります。あなただけの、その後の行の間の仕切りを追加したい場合は

divider_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <size android:height="1dp" /> 
    <solid android:color="@color/list_divider" /> 
</shape> 
10

TableRow division良い解決策を持っています。

<TableLayout  
    android:divider="?android:attr/dividerHorizontal" 
    android:showDividers="middle" 
    ... 
+0

Thnx ...素晴らしい解決策...魅力のような...! –

関連する問題