【Android】安卓开发实战之自定义分隔线条的长度、宽度、颜色

默认的分隔线条在长度上会充满整个屏幕,有时候我们需要留出一定距离来优化界面,实现方法如下:

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>

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值