在android中创建布局时,发现有些控件之间加一些分割线,会很美观,上网搜索了下,找到了三种方式创建分割线,下面就来分别来试一下。
1. 使用View
也是最简单的一种方式,直接定义宽度和高度,设置颜色即可。
但是,分割线较多的布局中,这种不太适合,会占用较多内存
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#303F9F"/>
2. 使用ImageView
方法与View类似,也是设置高度、宽度和颜色即可
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorAccent"/>
3. 自定义xml
自定义一个分割线的divider.xml,放置drawable目录下
使用时,一般在垂直布局中设置,水平布局中不能显示
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:divider="@drawable/divider"
android:showDividers="end"
android:dividerPadding="1.5dp">
...
注意点:
(1)垂直布局
(2)android:divider="@drawable/divider" ,不能直接设置颜色,否则不显示,divider就是自定义的xml
(3)android:showDividers=“end” 设置显示位置
end:末端
beginning:前端
middle:中间
none:不显示
(4)android:dividerPadding&