带分割线的TextView和LinearLayout

写ui的时候,总是有很多类似菜单列表类的界面要写,例如微信的界面,每一项都有一个分割线来分割,每一个大项又区分开来。

这里写图片描述

一般的做法都是用一个view标签,设置其背景颜色,高度,等等。这么做固然可以,虽说会消耗资源,但最恶心的还是代码的维护方面,若是界面不变还好,若界面总是要增加减少某个项,而分割线和这项又不在一块,总是需要把分割线和这个项隐藏,还可能要设置 layout_marginTop,简直是无穷无尽的噩梦。

其实线性布局自带分割线的属性,用法也很简单,但是是针对子布局的分割线,很不适用~
现在自定义组件在ondraw方法里面绘制分割线,并且可以自己定义颜色,高度,显示上面还是下面

用法如下:

 <com.example.xingyun.androidtestdemo.DividerTextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:background="#eee"
            android:text="你好"
            app:lineColor="#f00"
            app:lineWidth="2dp"
            app:showStyle="top"

            />

<com.example.xingyun.androidtestdemo.DividerLinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      app:lineColor="#000"
      app:showStyle="all"
      >
      <TextView
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:gravity="center"
          android:text="你好"
          />
</com.example.xingyun.androidtestdemo.DividerLinearLayout>

效果如下
这里写图片描述

show code

public class DividerLinearLayout extends LinearLayout {

    public static final short TOP = 1;
    public static final short BOTTOM= 2;
    public static final short ALL = 3;

    private Paint paint;
    /**
     * 默认线宽为1px
     */
    private float lineWidth = 1;
    /**
     * 默认线的颜色为浅灰 #D0D0D0
     */
    private int lineColor = 0xffD0D0D0;
    /**
     * 默认上下都显示
     */
    private int showStyle = ALL;

    public DividerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        paint = new Paint();

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DividerLinearLayout);
        lineColor = a.getColor(R.styleable.DividerLinearLayout_lineColor, 0xffD0D0D0);
        lineWidth = a.getDimensionPixelSize(R.styleable.DividerLinearLayout_lineWidth, 1);
        showStyle = a.getInt(R.styleable.DividerLinearLayout_showStyle, 3);
        a.recycle();

        paint.setColor(lineColor);
        paint.setStrokeWidth(lineWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showStyle==TOP){
            canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint);
        }else if (showStyle==BOTTOM){
            canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint);
        }else if (showStyle==ALL){
            canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint);
            canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint);
        }
    }
}
public class DividerTextView extends TextView {

    public static final short TOP = 1;
    public static final short BOTTOM= 2;
    public static final short ALL = 3;

    private Paint paint;
    /**
     * 默认线宽为1px
     */
    private float lineWidth = 1;
    /**
     * 默认线的颜色为浅灰 #D0D0D0
     */
    private int lineColor = 0xffD0D0D0;
    /**
     * 默认上下都显示
     */
    private int showStyle = ALL;

    public DividerTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DividerLinearLayout);
        lineColor = a.getColor(R.styleable.DividerLinearLayout_lineColor, 0xffD0D0D0);
        lineWidth = a.getDimensionPixelSize(R.styleable.DividerLinearLayout_lineWidth, 1);
        showStyle = a.getInt(R.styleable.DividerLinearLayout_showStyle, 3);
        a.recycle();

        paint.setColor(lineColor);
        paint.setStrokeWidth(lineWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showStyle==TOP){
            canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint);
        }else if (showStyle==BOTTOM){
            canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint);
        }else if (showStyle==ALL){
            canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint);
            canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint);
        }
    }
}
<declare-styleable name="DividerLinearLayout">
        <attr name="lineWidth" format="dimension"/>
        <attr name="lineColor" format="reference|color"/>
        <attr name="showStyle" format="enum">
            <enum name="top" value="1"/>
            <enum name="bottom" value="2"/>
            <enum name="all" value="3"/>
        </attr>
</declare-styleable>
原文地址:https://www.cnblogs.com/xingyun1992/p/7286561.html