有时候需要为界面设置分割线 如图:
我经常用的两种方法:
在布局中加入view
<View
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintBottom_toTopOf="@+id/ll_bottom"
android:background="#DBD8D8"/>
使用drawable资源文件
如果给控件加上边框可以使用如下代码
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:color="#ffa231"
android:width="2dp"
></stroke>
</shape>
但是使用这种方法缺点在于无法实现单边边框
因此可以采用layer-list的方式
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-4dp"
android:right="-4dp"
android:top="-4dp">
<shape>
<stroke
android:width="1dp"
android:color="#969494"/>
</shape>
</item>
</layer-list>
稍微解释以下,layer-list实现边框的方式,具体那条变显示哪表边不显示是通过如下代码控制的
android:left=""
android:right=""
android:top=""
android:bottom=""
它们的值加上stoke李width的值如果依旧为负,将不会显示。换言之就是加上width的值后为<=0不显示,>0就显示,显示的宽度也是加上width后的值。
如果需要底部显示,且宽度为2dp,那么可以将其他三边设为-2dp,width设为2dp