指定控件的边框线

控件指定方向显示边框xian

来自:http://blog.csdn.net/llew2011/article/details/30283821

import android.annotation.SuppressLint;
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.TextView;

/**
 * Description : 带有指定边框线的TextView
 * 若是四个边显示的话可以直接使用shape设置背景
 */

@SuppressLint("AppCompatCustomView")
public class BorderTextView extends TextView {
    /**
     * 四周是否带有边框
     */
    boolean borders = false;
    /**
     * 左边是否带有边框
     */
    boolean borderLeft = false;
    /**
     * 顶部是否带有边框
     */
    boolean borderTop = false;
    /**
     * 右侧是否带有边框
     */
    boolean borderRight = false;
    /**
     * 底部是否带有边框
     */
    boolean borderBottom = false;

    /**
     * 边框颜色(默认黑色)
     */
    String textColor = "#ff000000";
    public BorderTextView(Context context) {
        this(context, null);
    }

    public BorderTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public BorderTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // 获取自定义属性集
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView);
        // 是否设置全部边框,默认为false
        borders = typedArray.getBoolean(R.styleable.BorderTextView_layout_borders, false);
        // 是否设置左侧边框,默认为false
        borderLeft = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderLeft, false);
        // 是否设置顶部边框,默认为false
        borderTop = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderTop, false);
        // 是否设置右侧边框,默认为false
        borderRight = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderRight, false);
        // 是否设置底部边框,默认为false
        borderBottom = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderBottom, false);
        // 获取文本颜色值,用来画边框的,便于和文本颜色匹配
        textColor = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textColor");
        typedArray.recycle();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // 创建画笔
        Paint paint = new Paint();
        // 获取该画笔颜色
        int color = paint.getColor();
        // 设置画笔颜色
        paint.setColor(Color.parseColor(textColor));
        // 如果borderstrue,表示左上右下都有边框
        if (borders) {
            canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
            canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
            canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
                    this.getHeight() - 1, paint);
            canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
                    this.getHeight() - 1, paint);
        } else {
            if (borderLeft) {
                // 画左边框线
                canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
            }
            if (borderTop) {
                // 画顶部边框线
                canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
            }
            if (borderRight) {
                // 画右侧边框线
                canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
            }
            if (borderBottom) {
                // 画底部边框线
                canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
            }
        }
        // 设置画笔颜色归位
        paint.setColor(color);
    }
}

 然后在布局中直接引用:

 在最外层布局中添加

xmlns:androidborder="http://schemas.android.com/apk/res/com.gm.bordertextview"
androidborder是命名 ,后面再跟上包名,在控件引用的时候就可以直接引用属性了

<com.gm.bordertextview.BorderTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="左侧带有边框"
    android:layout_margin="10dip"
    androidborder:layout_borderLeft="true"
    android:textSize="20sp"
    android:textColor="#aabbcc">
</com.gm.bordertextview.BorderTextView>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值