1.通过增加控件方式显示边线
<View
android:layout_height="0.5dp"
android:layout_width="match_parent"
android:background="#d9d9d9"
/>
这种方式通过控制View的宽和高来实现,简单使用。
2.通过shap样式中stroke属性实现
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="0.5dp"
android:color="#d9d9d9"
/>
</shape>
这种适合给视图四周都需要边线的组件,如果只需要上、下需要而左、右不需要就没法实现了。
3.通过layer-list样式实现
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 边框 -->
<item>
<shape >
<!-- 通过pading指定边线有无和宽度 -->
<padding
android:top="0.5dp"
android:bottom="0.5dp"
/>
<solid
android:color="#d9d9d9"
/>
</shape>
</item>
<!-- 主体 -->
<item>
<shape >
<solid
android:color="#ffffff"
/>
</shape>
</item>
</layer-list>
如上,在控件设置backgroud,便实现了上下各添加0.5dp宽的边线,而左右却无,边线颜色#d9d9d9, 主体背景#ffffff。但这种方式,同样也有缺陷,无法实现一些设计师给你设计的边线不但头的情况。这种设计一般用在设置页面的条目里。
若想实现一部分边线不到头一部分到头的情况,便可以考虑在组件中以添加分隔线来实现,比如在ListView添加自定义的分隔线,然后把组件添加到ListView中,RecyclerView就更不用提了。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 边框 -->
<item>
<shape >
<!-- 通过pading指定边线有无和宽度 -->
<padding
android:left="10dp"
android:right="10dp"
/>
<!-- 两边颜色透明 -->
<solid
android:color="#00000000"
/>
</shape>
</item>
<!-- 主体 -->
<item>
<!-- 这里便是分隔线的颜色 -->
<shape >
<solid
android:color="#d9d9d9"
/>
</shape>
</item>
</layer-list>
如上,给Listview设置devider便可以实现那种不到头的分隔线。
除此之外,也可以LinearLayout自带的功能来实现分割线效果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/shape_divider_line"
android:showDividers="middle"
android:dividerPadding="5px"
android:orientation="vertical">