Android怎么实现Linearlayout的垂直分割线?

如题,就是在两个元素之间有一根有色彩的线... 如题,就是在两个元素之间有一根有色彩的线 展开
 我来答
千锋教育
2015-12-05 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部

方法一:使用一个View设置background属性放置在两个控件之间

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <View 
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@drawable/divider"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


但是这个有明显的缺点就是需要手动去添加一个View很繁琐


方法二:Android3.0及以上版本使用LinearLayout的android:showDividers属性

android:showDividers属性可以设置如下4个值:

none:不显示分隔线;

beginning:在LinearLayout的开始处显示分隔线;

end:在Linearlayout的结尾处显示分隔线;

middle:在LinearLayout中的每两个组件间显示分隔线


还要设置分割线的图片android:divider属性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:showDividers="middle"
    android:divider="@drawable/divider"
    android:gravity="center"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

可以看到每个button之间都有了分割线

百度网友1a235aba15
2015-07-08 · 超过80用户采纳过TA的回答
知道小有建树答主
回答量:147
采纳率:0%
帮助的人:118万
展开全部
你可以在layout中定义一个view,具体这样实现
<View
//把宽度设置成你要的宽度

//把高度设置你想要的线有多细,越高线越粗,根据自己需要定

//这是颜色

//当然位置自己定

/>

然后本就能实现分割线了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式