有时候需要向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"
<!--stroke的width决定了线的高度(即View的height属性) -->
android:width="3dp"/>
<!--size中指定的高度,决定了线这个图片整体的高度,必须大于等于stroke width -->
<!--指定了size属性时,line才能被正确绘制 -->
<size android:height="4dp"/>
</shape>