Android 如何在表格布局中的列之间设置分隔线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10073211/
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
How to set divider between columns in tablelayout?
提问by brig
I want to create a table with column dividers. I want to divide my columns with a vertical bar image. To achieve this I have used "android:divider="@drawable/abc"
but its not working. Below is my xml file for same:
我想创建一个带有列分隔符的表格。我想用垂直条形图像分隔我的列。为了实现这一点,我已经使用过,"android:divider="@drawable/abc"
但它不起作用。下面是我的 xml 文件:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:stretchColumns="*"
android:padding="5dip"
android:divider="@drawable/tracking_green"
>
<TableRow >
<TextView
android:id="@+id/retaileritem1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="xxxxxxx" />
<TextView
android:id="@+id/retaileritem2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="xxxxxxx" />
<ImageView
android:id="@+id/retailerimage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tracking_green" />
</TableRow>
回答by Sonny Ng
Add android:showDividers="middle"
添加 android:showDividers="middle"
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:stretchColumns="*"
android:padding="5dip"
android:divider="@drawable/tracking_green"
android:showDividers="middle"
>
回答by Mohammed Junaid
It's quite late to answer this question but here is the correct way to it.
回答这个问题为时已晚,但这是正确的方法。
For dividers between rows :
对于行之间的分隔符:
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@color/colorPrimary"
android:showDividers="middle">
and for dividers between columns:
对于列之间的分隔符:
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/colorPrimary"
android:showDividers="middle">
Explaination:
说明:
The divider tag in <TableLayout>
is used to place divider between it's direct childrens i.e Rows.
分隔符标签 in<TableLayout>
用于将分隔符放置在它的直接子级之间,即Rows。
While the divider tag in <TableRow>
is used to place divider between it's direct childrens i.e Columns
虽然分隔符标签<TableRow>
用于在它的直接子项之间放置分隔符,即列
回答by Devrath
This is how I did it !
我就是这样做的!
CODE
代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingBottom="50dp"
android:text="Salary Details"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:shrinkColumns="0"
android:stretchColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" >
<TextView
android:layout_gravity="center_vertical"
android:gravity="left"
android:text="Financial Year"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spnSearchByCity"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:entries="@array/year"
tools:listitem="@android:layout/simple_spinner_dropdown_item" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d30059"
android:orientation="vertical"
android:paddingTop="50dp" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Month"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Net Salary"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Click"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Jan"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="11305"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDetailsOneId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:text="DETAILS"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Feb"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="11405"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDetailsTwoId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:text="DETAILS"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp" >
<Button
android:id="@+id/btnMainMenuId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="MAIN MENU"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
Snapshot
快照
回答by YasirAzgar
What i wanted is a table like this
我想要的是这样的桌子
I added this in my styles.xml
我在我的styles.xml中添加了这个
<style name="Divider">
<item name="android:layout_width">1dip</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@color/divider_color</item>
</style>
<style name="Divider_invisible">
<item name="android:layout_width">1dip</item>
<item name="android:layout_height">match_parent</item>
</style>
Then in my table layout
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#92C94A" >
<TextView
android:id="@+id/textView11"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider_invisible" />
</LinearLayout>
<TextView
android:id="@+id/textView12"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/main_wo_colon"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
<TextView
android:id="@+id/textView13"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/side_wo_colon"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
<TextView
android:id="@+id/textView14"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/total"
android:textColor="@color/white"
android:textSize="16sp" />
</TableRow>
<!-- display this button in 3rd column via layout_column(zero based) -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6F9C33" >
<TextView
android:id="@+id/textView21"
android:padding="5dp"
android:text="@string/servings"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
..........
.......
......
回答by Yoon
try set the second textview marginleft=1dp. if you don't want divider set marginleft = 0dp
尝试设置第二个 textview marginleft=1dp。如果你不想要分隔线设置 marginleft = 0dp
回答by ornay odder
since tablelayout does not provide this directly, one posibble way might be a framelayout. place the desired table above another background_table wich only have one row and "match_parent" for height/width. add 3 columns with desired layout_weights. add vertical divider-imageview to second column. the main table should also use the same layout_weihts +margin in it`s rows to look good.
由于 tablelayout 不直接提供此功能,因此一种可能的方式可能是框架布局。将所需的表格放在另一个 background_table 上方,其中只有一行,高度/宽度为“match_parent”。添加具有所需 layout_weights 的 3 列。将垂直分隔线图像视图添加到第二列。主表也应该在它的行中使用相同的 layout_weihts +margin 以看起来不错。
horizontal divider possibly could be realized by adding an imageview between the "to-be-separated" tablerows.
水平分隔线可能可以通过在“待分离”的表格行之间添加一个图像视图来实现。
not tested yet, just an approach
尚未测试,只是一种方法
ornay
奥奈
回答by Xavier Egea
In a list with a vertical divider use the ImageView as follows:
在带有垂直分隔线的列表中,使用 ImageView 如下:
<ImageView
android:id="@+id/divider"
android:contentDescription="@string/divider"
android:layout_width="1dp"
android:layout_height="65dp"
android:layout_marginTop="8sp"
android:background="#3B3B3B"
/>
To avoid showing a divider when there aren't elements in the list you can delete the android:background="#3B3B3B"
line and set it programmatically:
为了避免在列表中没有元素时显示分隔线,您可以删除该android:background="#3B3B3B"
行并以编程方式设置它:
((ImageView) view.findViewById(R.id.divider)).setBackgroundColor(Color.parseColor("#3B3B3B"));