默认的分隔线条在长度上会充满整个屏幕,有时候我们需要留出一定距离来优化界面,实现方法如下:
1、在drawable文件夹下新建一个divider.xml文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--android:left 是定义分隔线距离屏幕左边的距离-->
<item android:left="@dimen/normal_padding_margin">
<shape android:shape="rectangle">
<!--这里是定义分隔线的颜色-->
<solid android:color="@color/theGray"/>
<!--这里是定义分隔线的高度-->
<size android:height="1dp"/>
</shape>
</item>
</layer-list>
2、在布局文件中使用这个资源文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundGray"
>
<!--使用布局文件就是在这两句话中添加-->
<!--android:showDividers="middle"-->
<!--android:divider="@drawable/divider" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/rectangle_width"
android:showDividers="middle"
android:divider="@drawable/divider"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setNameLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/name"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_nameId"
android:text="乔峰"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setSexLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/sex"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_sexId"
android:text="男"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setApartmentLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/apartment"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_apartmentId"
android:text="品牌部"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setGroupLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/group"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_groupId"
android:text="战略组"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/rectangle_width"
android:showDividers="middle"
android:divider="@drawable/divider"
android:orientation="vertical"
android:background="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setIntegralLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/integral"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_integralId"
android:text="100"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_padding_margin"
android:id="@+id/setFansLL"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/fans"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_fansId"
android:text="100"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theGray"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/rectangle_width"
android:background="@color/white"
>
<TextView
android:padding="@dimen/normal_padding_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myCollection"
android:textSize="@dimen/myTextSize"
android:textColor="@color/theBlack"
android:id="@+id/my_collectionId"
/>
</LinearLayout>
</LinearLayout>
效果: