2012-04-21 49 views
16

Android Design - Lists页面描述了“部分分隔线”。我知道你可以使用addHeaderView() a ListView来达到类似的效果。我想使用“分区分隔符”,而不使用ListView,而是使用LinearLayout添加“部分分隔线”到我的布局?

在下面的屏幕截图中,我指的是蓝色文本“电话”和“电子邮件”,它下面还有一行。该截图是从Android Design - Text Fields

enter image description here

我如何去将它添加到我的布局?它只是一个TextView加一条水平线?

+2

使用底部的一行创建背景图像,并将其设置为TextView的背景。 – 2012-04-21 07:30:37

+0

@mirroredAbstraction你应该写这个答案,因为它似乎是目前为止最好的解决方案。 – 2012-04-21 07:32:31

+0

可能重复的[Android的绘图分隔/分隔线在布局?](http://stackoverflow.com/questions/5049852/android-drawing-separator-divider-line-in-layout) – 2016-01-15 11:02:00

回答

42

我正在寻找同样的问题。 我发现了一个简单的方法来告诉应用程序,一个texview是一款分隔符:

<TextView 
    android:id="@+id/address_label" 
    style="?android:attr/listSeparatorTextViewStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Address"/> 

行:

style="?android:attr/listSeparatorTextViewStyle" 

下划线添加到文字,样式它accordingli到defaulf“分隔符“主题。

+5

如何更改下划线的颜色? – 2013-12-24 19:25:19

-1

TextView 2dp的高度和宽度=匹配父项,并将背景色设置为想要的线条的颜色。

您也可以通过颠倒两个设置来做垂直。

4

尝试将此视图放在“手机”的TextView之后。在这个视图中,我已经放入了一个背景颜色,你可以改变你的愿望。祝你好运。

<View 
    android:layout_width="match_parent" 
    android:layout_height="2dip" 
    android:background="#FF909090" /> 
+0

请在这里发布您的代码,而不是在pastebin上。 – 2012-09-15 13:19:26

+0

这对我很好。谢谢! – 2013-01-28 13:55:48

+2

只是一个简短的评论..有了这个解决方案,您应该使用px而不是dip,因为在较小的屏幕中,它可以转换为0px并变得不可见。 – Thpramos 2014-12-15 00:41:00

2

在这个问题上的提问者的要求,我写我的评论作为一个答案

创建在底部的线条背景图像,并将其设置为 背景,你的TextView 。

3

将溶液最后不得不一个可包含布局称为util_horizontal_line_section.xml

<?xml version="1.0" encoding="utf-8"?> 

<View 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="1dip" 
    android:background="@color/sectionSeparatorColour"/> 

随着values/colors.xmlsectionSeparatorColour条目:

<color name="sectionSeparatorColour">#a0a0a0</color> 

可包含的经由:

<include layout="@layout/util_horizontal_line_section"/>