LinearLayout增加分隔线

这篇博客介绍了如何在Android的LinearLayout中增加分隔线的两种方法:一种是通过放置ImageView并设置颜色或图形,另一种是利用android3.0后的属性showDividers和divider,详细说明了属性的设置和使用场景。

有时候需要向LinearLayout中增加分隔线,此时有两种做法。

1、可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形。
分隔线View的定义代码类似于:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorDivider"/>

2、在android3.0及后面的版本在LinearLayout里增加了分割线的属性,
此时的用法类似于:

...........
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:showDividers="middle"
        android:divider="@android:drawable/divider_horizontal_textfield">
...........

其中:android:showDividers属性可以在LinearLayout的相应位置显示分隔线。
android:showDividers属性可以设置如下4个值:
none:不显示分隔线;
beginning:在LinearLayout的开始处显示分隔线;
end:在Linearlayout的结尾处显示分隔线;
middle:在LinearLayout中的每两个组件间显示分隔线。

android:divider属性表示分隔线对应的图像,需要一个Drawable ID。
如果分割线是图片,那就直接引用图片就行;
如果要使用颜色就必须使用shape来显示,直接使用颜色或Color是没有用的。
使用shape时的代码类似于:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:color="@color/colorAccent"
        <!--strokewidth决定了线的高度(即Viewheight属性) -->
        android:width="3dp"/>

    <!--size中指定的高度,决定了线这个图片整体的高度,必须大于等于stroke width -->
    <!--指定了size属性时,line才能被正确绘制 -->
    <size android:height="4dp"/>
</shape>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值