Android 表格布局中的行之间的分隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11411421/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
separation between rows in table layout
提问by prakash .k
I am displaying one table layout, in that I want separation line between rows in the table.Also is it possible to have column wise separation in table layout.Please help me.
我正在显示一个表格布局,因为我希望表格中的行之间有分隔线。也可以在表格布局中按列进行分隔。请帮助我。
Following is my xml table layout:
以下是我的 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>
回答by mainu
Check this. It will work.
检查这个。它会起作用。
<?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>
回答by grantnz
If you only want to add a divider between rows then TableRow divisionhas a good solution.
如果您只想在行之间添加分隔线,那么TableRow 划分有一个很好的解决方案。
<TableLayout    
     android:divider="?android:attr/dividerHorizontal"
     android:showDividers="middle"
    ...
回答by ar34z
The TableLayoutextends LinearLayoutso you can add dividers in xml. I assume this would work back then:
该TableLayout扩展LinearLayout使您可以在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>
My divider looks like the following:
我的分隔线如下所示:
divider_list.xml
分隔列表.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>
回答by Vinay W
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);
回答by AMerle
I think you have to play with the background of the TableLayoutand the margin of your rows... 
我认为您必须使用行的背景TableLayout和边距...
回答by user4232
I have made some changes in your code and think this will help you...
我对您的代码进行了一些更改,并认为这会对您有所帮助...
    <?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>
Thanks....
谢谢....

