Android帶有邊框的RelativeLayout、LinearLayout

平常開發的時候總會遇到一些分割線,之前都是用一個View或者一個ImageView來繪製一條比較細的分割線,時間長了再去修改代碼,有的時候就去要看一下,也比較亂.所以就打算寫一個帶有邊框的ViewGroup方便日常開發.

一、代碼實現

代碼很簡單,直接放下代碼:
定義一下自定義屬性,在attr文件:

    <!--邊框的粗度-->
    <attr name="borderStrokeWidth" format="float"/>
    <!--邊框顏色-->
    <attr name="borderColor" format="color"/>
    <!--底邊邊線左邊斷開距離-->
    <attr name="borderBottomLeftBreakSize" format="dimension"/>
    <!--底邊邊線右邊斷開距離-->
    <attr name="borderBottomRightBreakSize" format="dimension"/>
    <!--是否需要上邊框-->
    <attr name="needTopBorder" format="boolean"/>
    <!--是否需要左右邊框-->
    <attr name="needLeftAndRigtBorder" format="boolean"/>
    <!--是否需要下邊框-->
    <attr name="needBottomBorder" format="boolean"/>
    <declare-styleable name="BorderRelativeLayout">
        <attr name="borderStrokeWidth"/>
        <attr name="borderColor"/>
        <attr name="borderBottomLeftBreakSize"/>
        <attr name="borderBottomRightBreakSize"/>
        <attr name="needTopBorder"/>
        <attr name="needLeftAndRigtBorder"/>
        <attr name="needBottomBorder"/>
    </declare-styleable>

定義一個BorderRelativeLayout繼承自RelativeLayout:

package com.junweiliu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.junweiliu.R;

/**
 * Created by junweiliu on 16/6/3.
 */
public class BorderRelativeLayout extends RelativeLayout {
    /**
     * 畫筆
     */
    private Paint mPaint;
    /**
     * 邊框顏色
     */
    private int mPaintColor;
    /**
     * 邊框粗細
     */
    private float mBorderStrokeWidth;
    /**
     * 底邊邊線左邊斷開距離
     */
    private int mBorderBottomLeftBreakSize;
    /**
     * 底邊邊線右邊斷開距離
     */
    private int mBorderBottomRightBreakSize;
    /**
     * 是否需要上邊框
     */
    private boolean isNeedTopBorder;
    /**
     * 是否需要左右邊框
     */
    private boolean isNeedLeftAndRightBorder;
    /**
     * 是否需要下邊框
     */
    private boolean isNeedBottomBorder;


    public BorderRelativeLayout(Context context) {
        this(context, null);
    }

    public BorderRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 獲取自定義屬性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BorderRelativeLayout);
        mPaintColor = ta.getColor(R.styleable.BorderRelativeLayout_borderColor, Color.GRAY);
        mBorderStrokeWidth = ta.getFloat(R.styleable.BorderRelativeLayout_borderStrokeWidth, 2.0f);
        mBorderBottomLeftBreakSize = ta.getDimensionPixelSize(R.styleable.BorderRelativeLayout_borderBottomLeftBreakSize, 0);
        mBorderBottomRightBreakSize = ta.getDimensionPixelSize(R.styleable.BorderRelativeLayout_borderBottomRightBreakSize, 0);
        isNeedTopBorder = ta.getBoolean(R.styleable.BorderRelativeLayout_needTopBorder, true);
        isNeedLeftAndRightBorder = ta.getBoolean(R.styleable.BorderRelativeLayout_needLeftAndRigtBorder, false);
        isNeedBottomBorder = ta.getBoolean(R.styleable.BorderRelativeLayout_needBottomBorder, true);
        ta.recycle();
        init();

    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mPaintColor);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mBorderStrokeWidth);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        //  畫4個邊
        if (isNeedTopBorder) {
            canvas.drawLine(0, 0, this.getWidth(), 0, mPaint);
        }
        if (isNeedBottomBorder) {
            canvas.drawLine(mBorderBottomLeftBreakSize, this.getHeight(), this.getWidth() - mBorderBottomRightBreakSize, this.getHeight(), mPaint);
        }
        if (isNeedLeftAndRightBorder) {
            canvas.drawLine(0, 0, 0, this.getHeight(), mPaint);
            canvas.drawLine(this.getWidth(), 0, this.getWidth(), this.getHeight(), mPaint);
        }
    }

    /**
     * 設置邊框顏色
     *
     * @param color
     */
    public void setBorderColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    /**
     * 設置邊框寬度
     *
     * @param size
     */
    public void setBorderStrokeWidth(float size) {
        mPaint.setStrokeWidth(size);
        invalidate();
    }


    /**
     * 設置是否需要頂部邊框
     *
     * @param needtopborder
     */
    public void setNeedTopBorder(boolean needtopborder) {
        isNeedTopBorder = needtopborder;
        invalidate();
    }

    /**
     * 設置是否需要底部邊框
     *
     * @param needbottomborder
     */
    public void setNeedBottomBorder(boolean needbottomborder) {
        isNeedBottomBorder = needbottomborder;
        invalidate();
    }
}

二、使用方法

來使用一下,我們可以在平常的佈局中使用,比如在一個類似個人信息的頁面,代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:br="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical"
        >
        <!--第一行-->
        <com.junweiliu.widget.BorderRelativeLayout
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:gravity="center_vertical"
            br:borderBottomLeftBreakSize="16dp"
            br:borderBottomRightBreakSize="16dp"

            br:borderColor="#eeeeee"
            br:borderStrokeWidth="3.0"
            >

            <TextView
                android:id="@+id/tv_balance_txt"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="16dp"
                android:gravity="center"
                android:text="第一行"
                android:textSize="15sp"
                />

            <ImageView
                android:id="@+id/iv_ia_prompt"
                android:layout_width="14dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="16dp"             
                android:layout_toRightOf="@+id/tv_balance_txt"
                android:src="@mipmap/ic_launcher"
                />

            <TextView
                android:id="@+id/tv_balance_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="16dp"
                android:layout_marginTop="24dp"
                android:textColor="#333333"
                android:text="第一行內容"
                android:textSize="12sp"
                />
        </com.junweiliu.widget.BorderRelativeLayout>
        <!--第二行-->
        <com.junweiliu.widget.BorderRelativeLayout
            android:layout_width="match_parent"
            android:layout_height="67dp"
            android:gravity="center_vertical"
            br:borderColor="#eeeeee"
            br:borderStrokeWidth="3.0"
            br:needTopBorder="false"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="16dp"
                android:gravity="center"
                android:text="第二行"
                android:textSize="15sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="16dp"
                android:gravity="center"
                android:text="第二行內容"
                android:textColor="#333333"
                android:textSize="12sp"
                />
        </com.junweiliu.widget.BorderRelativeLayout>
    </LinearLayout>
</LinearLayout>

效果圖如下:

效果圖1

在ListView中也經常會使用到分割線,比如帶有頭部分割線的ListView,使用方法如下:
先寫一個佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:br="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/white"
              android:orientation="vertical">

    <com.junweiliu.widget.BorderRelativeLayout
        android:id="@+id/br_ia_detail"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        br:borderColor="#333333"
        br:borderStrokeWidth="2.0"
        br:needTopBorder="false"
        >
        <!--上邊部分-->
        <TextView
            android:id="@+id/tv_ia_detail_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="16sp"
            android:layout_marginTop="15dp"
            android:text="第一行"
            android:textSize="15sp"
            />

        <TextView
            android:id="@+id/tv_ia_detail_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:text="第一行內容"
            android:textSize="15sp"
            />
        <!--下邊部分-->
        <TextView
            android:id="@+id/tv_ia_detail_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="12dp"
            android:text="第二行"
            android:textSize="11sp"
            />

        <TextView
            android:id="@+id/tv_ia_detail_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="12dp"
            android:text="第二行內容"
            android:textSize="11sp"
            />
    </com.junweiliu.widget.BorderRelativeLayout>

</LinearLayout>

綁定數據部分就不去寫了,只需要在適配器中進行判斷,就能夠添加頭部分割線了.在適配器getView部分添加:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ...

        // 如果是第一個位置顯示頂部邊框
        BorderRelativeLayout br = (BorderRelativeLayout) convertView.findViewById(R.id.br_ia_detail);
        if (0 == position) {
            br.setNeedTopBorder(true);
        } else {
            br.setNeedTopBorder(false);
        }
        return convertView;
    }

效果圖如下:

效果圖2

使用比較簡單,用法也比較多變,如果需要LinearLayout,只需改變一下繼承關係就可以了.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章