最近写了一个带边框的TextView,因为项目中有许多地方需要用到一个带边框的TextView,而且需要根据不同的状态显示不同的边框颜色,当时的第一反应就是用shape做,但后来转念一想这要需要定义多少个drawable文件啊,瞬间决定换个思路,于是自定义一个TextView带有边框,并且能通过java代码来动态控制边框以及字体颜色,这样就会省好多事情,上次也写了一个圆角实体的背景颜色,具体可以查看这篇博客:Android自定义TextView带圆角及背景颜色(动态改变圆角背景颜色),这次就写了一个空心的带边框的,好了,老规矩,先来一波效果图,毕竟有效果图才知道是不是想要的那种效果,具体看效果图,左边是布局文件视图看到的,右边是通过代码设置的效果:
接下来就来看看代码是怎么实现的,代码实现是非常简单,就是自定义了一个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中有定义:
<declare-styleable name="TextViewBorder">
<attr name="borderColor" format="color"/>
</declare-styleable>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.withborderstextview.TextViewBorder
android:id="@+id/state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="3dp"
android:layout_margin="10dp"
android:text="状态1(xml设置)"
android:textColor="@color/app_blue_color"
app:borderColor="@color/app_blue_color" />
<com.test.withborderstextview.TextViewBorder
android:id="@+id/state2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="3dp"
android:layout_margin="10dp"
android:text="状态2(java代码动态更改)"
android:textColor="@color/app_blue_color"
app:borderColor="@color/app_blue_color" />
<com.test.withborderstextview.TextViewBorder
android:id="@+id/state3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="3dp"
android:layout_margin="10dp"
android:text="状态3(java代码动态更改)"
android:textColor="@color/app_blue_color"
app:borderColor="@color/app_blue_color" />
<com.test.withborderstextview.TextViewBorder
android:id="@+id/state4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:padding="3dp"
android:text="状态4(java代码动态更改)"
android:textColor="@color/app_blue_color"
app:borderColor="@color/app_blue_color" />
</LinearLayout>
最后我们来看看怎么在代码中动态更改边框以及字体颜色
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带圆角边框颜色(动态更改边框颜色)