2012-07-10 9 views
11

Tôi đang hiển thị một bố cục bảng, trong đó tôi muốn tách dòng giữa các hàng trong bảng. Ngoài ra, có thể có cột phân tách khôn ngoan trong bố cục bảng.Xin vui lòng trợ giúp tôi.cách nhau giữa các hàng trong bố cục bảng

Sau đây là cách bố trí bảng xml của tôi:

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

Như u đang thêm 2 văn bản trong dòng của bảng, thêm một hình ảnh (dòng)/xem như chia giữa văn bản có 2dp.this chiều rộng sẽ làm việc như chia – AkashG

+0

bạn cũng có thể thử loại này. http://stackoverflow.com/questions/2108456/how-can-i-create-a-table-with-borders-in-android hoặc http://stackoverflow.com/questions/7117533/how-to-give-border-of-tế ​​bào-in-tablelayout-in-android – spr

Trả lời

25

Kiểm tra điều này. Nó sẽ hoạt động.

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

Chúng tôi có thể thực hiện cùng một chương trình không? gây ra tôi đang tạo bố cục dựa trên lập trình. – AndroidOptimist

+2

Tôi nghĩ đây không phải là cách viết để thực hiện nó. nếu bạn có 20 hàng bảng thì bạn phải tạo thêm 20 hàng nữa cho bộ chia ..... Tôi nghĩ có thẻ có tên là "android: devider" trong TableLayout .. nhưng tôi không biết cách sử dụng nó ... nếu một số cơ thể biết xin vui lòng cho biết sớm ... –

+0

Đừng quên thêm 'android: layout_span =" 2 "' vào chế độ xem chia. – 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

Cảm ơn Vinay Wadhwa ..Bạn có thể cho tôi biết tôi phải thêm cái này ở đâu không ?? –

+0

Thêm chế độ xem trong xml của bạn giữa hai lần xem văn bản thay vì thực hiện một cách chuyên nghiệp – AkashG

+0

bạn có thể thêm mã này vào hoạt động sử dụng xml bạn đã đề cập trong câu hỏi của mình. –

1

Tôi nghĩ rằng bạn phải chơi với nền của TableLayout và lề hàng của bạn ...

+0

Đây là cách tôi đã làm nó, quá, kiến ​​nó là giải pháp tốt nhất bởi vì nó tránh sưng lên trong bố cục. – olefevre

0

Tôi đã thực hiện một số thay đổi trong mã của bạn và nghĩ rằng điều này sẽ giúp bạn ...

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

Cảm ơn ....

+0

Cảm ơn người dùng4232..Không có gì sắp tới .. bố cục có thể bị thu hẹp .. –

3

Mở rộng TableLayout mở rộng LinearLayout để bạn có thể thêm bộ chia trong xml. Tôi cho rằng điều này sẽ làm việc trở lại sau đó:

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

chia của tôi trông giống như sau:

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

Nếu bạn chỉ muốn thêm một chia giữa các hàng sau đó TableRow division có một giải pháp tốt.

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

Thnx ... giải pháp tuyệt vời..làm việc như quyến rũ ... !! –