android code修改border颜色,Android自定义TextView边框颜色(动态改变边框颜色以及字体颜色)...

最近写了一个带边框的TextView,因为项目中有许多地方需要用到一个带边框的TextView,而且需要根据不同的状态显示不同的边框颜色,当时的第一反应就是用shape做,但后来转念一想这要需要定义多少个drawable文件啊,瞬间决定换个思路,于是自定义一个TextView带有边框,并且能通过java代码来动态控制边框以及字体颜色,这样就会省好多事情,上次也写了一个圆角实体的背景颜色,具体可以查看这篇博客:Android自定义TextView带圆角及背景颜色(动态改变圆角背景颜色),这次就写了一个空心的带边框的,好了,老规矩,先来一波效果图,毕竟有效果图才知道是不是想要的那种效果,具体看效果图,左边是布局文件视图看到的,右边是通过代码设置的效果:

5f891e7da4173eb645511a1803965d2e.png          273d69768618d1aeabcb649b1dc355d0.png

接下来就来看看代码是怎么实现的,代码实现是非常简单,就是自定义了一个TextView,然后引用就行了,先看看自定义的TextView代码:

TextViewBorder.java

package com.test.withborderstextview;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.widget.TextView;

/**

* Created by lp on 2016/9/21.

*/

public class TextViewBorder extends TextView {

private static final int STROKE_WIDTH = 2;

private int borderCol;

private Paint borderPaint;

public TextViewBorder(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray a = context.getTheme().obtainStyledAttributes(attrs,

R.styleable.TextViewBorder, 0, 0);

try {

borderCol = a.getInteger(R.styleable.TextViewBorder_borderColor, 0);//0 is default

} finally {

a.recycle();

}

borderPaint = new Paint();

borderPaint.setStyle(Paint.Style.STROKE);

borderPaint.setStrokeWidth(STROKE_WIDTH);

borderPaint.setAntiAlias(true);

}

@Override

protected void onDraw(Canvas canvas) {

if (0 == this.getText().toString().length())

return;

borderPaint.setColor(borderCol);

int w = this.getMeasuredWidth();

int h = this.getMeasuredHeight();

RectF r = new RectF(2, 2, w - 2, h - 2);

canvas.drawRoundRect(r, 5, 5, borderPaint);

super.onDraw(canvas);

}

public int getBordderColor() {

return borderCol;

}

public void setBorderColor(int newColor) {

borderCol = newColor;

invalidate();

requestLayout();

}

}

其中的style在style.xml中有定义:

接下来就看看怎么引用:这里需要注意引入 xmlns:app="http://schemas.android.com/apk/res-auto"

activity_main.xml

最后我们来看看怎么在代码中动态更改边框以及字体颜色

MainActivity.java

package com.test.withborderstextview;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

private TextViewBorder state1;

private TextViewBorder state2;

private TextViewBorder state3;

private TextViewBorder state4;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

state1 = (TextViewBorder) findViewById(R.id.state1);

state2 = (TextViewBorder) findViewById(R.id.state2);

state3 = (TextViewBorder) findViewById(R.id.state3);

state4 = (TextViewBorder) findViewById(R.id.state4);

//边框颜色

state2.setBorderColor(getResources().getColor(R.color.app_red_delete_color));

//字体颜色

state2.setTextColor(getResources().getColor(R.color.app_red_delete_color));

state3.setBorderColor(getResources().getColor(R.color.app_blue_color));

state3.setTextColor(getResources().getColor(R.color.progress_color));

state4.setBorderColor(getResources().getColor(R.color.app_red_delete_color));

state4.setTextColor(getResources().getColor(R.color.app_blue_color));

}

}

到这里就基本已经完成了,大家有什么建议给留言啊,需要进步,加油吧骚年

如果有不清楚的可以下载源码看看:

Android自定义TextView带圆角边框颜色(动态更改边框颜色)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值