最近根据项目需求自定义了一个TextView控件,主要用来做状态的标识,比如一个订单状态有各种,当然了这种设置在Android中可以直接用xml文件来处理,但是对于xml文件太过于麻烦,针对不同的颜色需要写一个xml文件,这样太繁琐了,所以就自己写了一个控件,只要动态设置一下属性就可以达到想要的目的,在此把这代码分享出来,当然了,也希望大家多多提意见,毕竟代码优化是一步一步跟进完善的,好了我们先来看看具体的效果图,毕竟有图才能看看是不是自己想要的那种控件,上图:

下面我们就来看看具体的实现吧

首先是自定义控件ColorTextView.java文件:

package com.test.colortextviewdemo;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.util.TypedValue;

import android.view.View;

/**

* 自定义申请状态textView

*/

public class ColorTextView extends View {

/**

* 文本内容

*/

private String mTitleText;

/**

* 文本的颜色

*/

private int mTitleTextColor;

/**

* 文本的大小

*/

private int mTitleTextSize;

private int ctvBackgroundColor;

/**

* 圆角大小

*/

private int mCornerSize;

/**

* 绘制时控制文本绘制的范围

*/

private Rect mtitleBound;

private Paint mtitlePaint;

public ColorTextView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public ColorTextView(Context context) {

this(context, null);

}

public void setCtvBackgroundColor(int ctvBackgroundColor) {

this.ctvBackgroundColor = ctvBackgroundColor;

}

/**

* 获得我自定义的样式属性

*

* @param context

* @param attrs

* @param defStyle

*/

public ColorTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

/**

* 获得我们所定义的自定义样式属性

*/

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorTextView, defStyle, 0);

int n = a.getIndexCount();

for (int i = 0; i < n; i++) {

int attr = a.getIndex(i);

switch (attr) {

case R.styleable.ColorTextView_ctvText:

mTitleText = a.getString(attr);

break;

case R.styleable.ColorTextView_ctvTextColor:

// 默认颜色设置为黑色

mTitleTextColor = a.getColor(attr, Color.BLACK);

break;

case R.styleable.ColorTextView_ctvTextSize:

// 默认设置为16sp,TypeValue也可以把sp转化为px

mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(

TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));

break;

case R.styleable.ColorTextView_ctvBackground:

//默认为白色

ctvBackgroundColor = a.getColor(attr, Color.WHITE);

break;

case R.styleable.ColorTextView_ctvCornerSize:

//默认圆角为0

mCornerSize = a.getInteger(attr, 0);

break;

}

}

a.recycle();

mtitlePaint = new Paint();

mtitlePaint.setTextSize(mTitleTextSize);

mtitleBound = new Rect();

mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int width;

int height;

if (widthMode == MeasureSpec.EXACTLY) {

width = widthSize;

} else {

mtitlePaint.setTextSize(mTitleTextSize);

mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);

int desired = getPaddingLeft() + mtitleBound.width() + getPaddingRight();

width = desired <= widthSize ? desired : widthSize;

}

if (heightMode == MeasureSpec.EXACTLY) {

height = heightSize;

} else {

mtitlePaint.setTextSize(mTitleTextSize);

mtitlePaint.getTextBounds(mTitleText, 0, mTitleText.length(), mtitleBound);

int desired = getPaddingTop() + mtitleBound.height() + getPaddingBottom();

height = desired <= heightSize ? desired : heightSize;

}

setMeasuredDimension(width, height);

}

@Override

protected void onDraw(Canvas canvas) {

Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);

paint.setAntiAlias(true);

paint.setColor(ctvBackgroundColor);

RectF rec = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());

canvas.drawRoundRect(rec, mCornerSize, mCornerSize, paint);

mtitlePaint.setColor(mTitleTextColor);

Paint.FontMetricsInt fontMetrics = mtitlePaint.getFontMetricsInt();

int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;

canvas.drawText(mTitleText, getPaddingLeft(), baseline, mtitlePaint);

}

}

其中自定义的style在attrs.xml文件中,具体看代码:

接下来我们就来看看是怎么使用的

activity_main.xml

xmlns:radiostyle="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_marginTop="15dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:padding="5dp"

radiostyle:ctvBackground="@color/app_theme_color"

radiostyle:ctvCornerSize="30"

radiostyle:ctvText="我是原文本"

radiostyle:ctvTextColor="@color/app_white"

radiostyle:ctvTextSize="15sp" />

android:layout_marginTop="15dp"

android:id="@+id/text_0"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:padding="5dp"

radiostyle:ctvBackground="@color/app_theme_color"

radiostyle:ctvCornerSize="30"

radiostyle:ctvText="我是自定义颜色文本1"

radiostyle:ctvTextColor="@color/app_white"

radiostyle:ctvTextSize="15sp" />

android:layout_marginTop="15dp"

android:id="@+id/text_1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:padding="5dp"

radiostyle:ctvBackground="@color/app_theme_color"

radiostyle:ctvCornerSize="30"

radiostyle:ctvText="我是自定义颜色文本2"

radiostyle:ctvTextColor="@color/app_white"

radiostyle:ctvTextSize="15sp" />

android:layout_marginTop="15dp"

android:id="@+id/text_2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:padding="5dp"

radiostyle:ctvBackground="@color/app_theme_color"

radiostyle:ctvCornerSize="30"

radiostyle:ctvText="我是自定义颜色文本3"

radiostyle:ctvTextColor="@color/app_white"

radiostyle:ctvTextSize="15sp" />

最后我们来看看是怎么在代码中改变背景颜色值的:

MainActivity.java

package com.test.colortextviewdemo;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

private ColorTextView ctv0;

private ColorTextView ctv1;

private ColorTextView ctv2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ctv0 = (ColorTextView) findViewById(R.id.text_0);

ctv1 = (ColorTextView) findViewById(R.id.text_1);

ctv2 = (ColorTextView) findViewById(R.id.text_2);

ctv0.setCtvBackgroundColor(getResources().getColor(R.color.progress_color));

ctv1.setCtvBackgroundColor(getResources().getColor(R.color.app_red));

ctv2.setCtvBackgroundColor(getResources().getColor(R.color.colorAccent));

}

}

好啦,使用就是这么简单,需要的可以在这个基础上进行完善,有什么好的建议可以给我留言,如果有需要的也可以点击下载源码:

Android自定义TextView带圆角及背景颜色

android背景颜色动态修改,Android自定义TextView带圆角及背景颜色(动态改变圆角背景颜色)...相关推荐

  1. Android Studio(五):修改Android Studio项目包名

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  2. android中弹出消息字体设置,Android如何设置中粗字体——自定义textview

    最近UI出了一版新的UI效果图,里面的标题用的都是中粗字体.这个中粗字体对于ios来说是很容易的,因为ios本省就自带中粗字体,但是对于安卓来说就没那么简单了,因为安卓中的textview只有标准字体 ...

  3. Android如何设置中粗字体——自定义textview

    最近UI出了一版新的UI效果图,里面的标题用的都是中粗字体.这个中粗字体对于ios来说是很容易的,因为ios本省就自带中粗字体,但是对于安卓来说就没那么简单了,因为安卓中的textview只有标准字体 ...

  4. android导航条高度修改,Android中修改TabLayout底部导航条Indicator长短的方法

    前言 对于Tablayout相信大家都不陌生,在开发中使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映射来修改自己想要的长短,其实也 ...

  5. android获取ro._修改Android序列号(Serial Number)

    文档说明 本文档以SC806-CN-00-71(msm8909平台 Android7系统)为例,描述如何修改Android Serial Number. Serial Number说明 菜单 Abou ...

  6. android代码图片编辑,怎样修改android系统apk软件里面的代码和图片?

    你好,你的问题我算是看明白了,从你的问题可以设计三个方面,apk反编译.apk回编译以及apk签名,看来,我得从头说起了. 首先,我在这里提供下反编译Android所需的软件,当然是全套,刚刚收集整理 ...

  7. android 反调试 github,修改Android手机内核,绕过反调试

    本文博客链接:http://blog..net/qq1084283172/article/details/57086486 0x1.手机设备环境 Model number: Nexus 5 OS Ve ...

  8. 动态代理大揭晓,带你彻底弄清楚动态代理

    前言 代理模式是一种设计模式,能够使得在不修改源目标的前提下,额外扩展源目标的功能.即通过访问源目标的代理类,再由代理类去访问源目标.这样一来,要扩展功能,就无需修改源目标的代码了.只需要在代理类上增 ...

  9. android签名忘记密码,修改Android签名证书keystore的密码、别名alias以及别名密码

    之前在测试Eclipse ADT的Custom debug Eclipse ADT的Custom debug keystore所需证书规格,提到过自定义调试证书的密码和 1. 首先当然是先复制一份正式 ...

最新文章

  1. 【前端】20款国外非常漂亮的优秀网站404错误页面HTML模板
  2. 印钞机 V1.0(量化选基总结)
  3. 【杂谈】学深度学习的你有GPU了吗
  4. Squid在企业中的应用
  5. python元组的概念_python元组的概念知识点
  6. protobuf在go中的应用
  7. 使用@Async实现异步调用
  8. 汉诺塔 X HDU - 2511
  9. Windows10 【系统周期表】【系统下载表】【大型软件表】
  10. 机房线路故障,引发多家公司不能上网,和自己de经历有感
  11. 四种方法下载网络文本数据到本地内存
  12. SQL Server 2014 导入Excel
  13. php 函数频率,这是一些使用频率比较高的php函数……
  14. Jquery中val、text、html的区别
  15. 你需要知道的关于铁氧体磁珠的一切
  16. python发短信sim800_sim800l 发短信
  17. 统考计算机应用基础ex,EXCEL操作题
  18. SCC(三):HEVC IBC
  19. 计算几何 - 你绝对找不到比这更好的计算几何
  20. 测试用例之因果图分析法

热门文章

  1. AI驱动智能化日志分析 : 通过决策树给日志做聚类分析
  2. 201503-2-数字排序
  3. F1-VmwareCentOS7.x
  4. thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?...
  5. 【perl】LWP module
  6. 关于ckeditor的配置
  7. SQL Server 2012安装错误案例:Error while enabling Windows feature: NetFx3, Error Code: -2146498298...
  8. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)
  9. asp.net c# 常见面试试题总结汇总(含答案)
  10. JSP九大内置对象的分析,对应SERVLET中如何获取、使用