重写TextView,实现圆形背景,文本居中显示
最近,在做考试试题排版,产品提出题号希望显示成圆形背景,序号文本居中显示。

(有点问题:文本没有绝对居中,暂时没做处理。)
为此,我采取的方式是重写TextView的onDraw方法,绘制一个圆形背景。
具体代码如下:
package com.example.myapp; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.TextView; /**
* 自定义形状的TextView 圆形 椭圆形
* Created by cjy on 16/11/30.
*/
public class CustomShapTextView extends TextView{
private Context mContext;
/**
* 画笔
*/
private Paint mPaint;
/**
* 画笔颜色 默认灰色
*/
private int mPaintNormalColor = 0xFFDCDCDC;
/**
* 画笔颜色 选中时的颜色,默认灰色
*/
private int mPaintSelectColor = 0xFFDCDCDC;
/**
* 是否填充颜色
*/
private boolean isFillColor = false; public CustomShapTextView(Context context) {
super(context);
} public CustomShapTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint(context,attrs);
} public CustomShapTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint(context,attrs);
} /**
* 初始化画笔和自定义属性
* @param context
* @param attrs
*/
private void initPaint(Context context,AttributeSet attrs){
mContext = context;
TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomShapTextView);
mPaintNormalColor = typeArray.getColor(R.styleable.CustomShapTextView_paintNormalColor,mPaintNormalColor);
mPaintSelectColor = typeArray.getColor(R.styleable.CustomShapTextView_paintSelectColor,mPaintSelectColor);
mPaint = new Paint();
} /**
* 调用onDraw绘制边框
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
//抗锯齿
mPaint.setAntiAlias(true);
if (isFillColor) {
//画笔颜色
mPaint.setColor(mPaintSelectColor);
mPaint.setStyle(Paint.Style.FILL);
}else{
//画笔颜色
mPaint.setColor(mPaintNormalColor);
//画笔样式:空心
mPaint.setStyle(Paint.Style.STROKE);
} //创建一个区域,限制圆弧范围
RectF rectF = new RectF();
//设置半径,比较长宽,取最大值
int radius = getMeasuredWidth() > getMeasuredHeight() ? getMeasuredWidth() : getMeasuredHeight();
//设置Padding 不一致,绘制出的是椭圆;一致的是圆形
rectF.set(getPaddingLeft(),getPaddingTop(),radius-getPaddingRight(),radius-getPaddingBottom());
//绘制圆弧
canvas.drawArc(rectF,0,360,false,mPaint); //最后调用super方法,解决文本被所绘制的圆圈背景锁覆盖的问题
super.onDraw(canvas);
} /**
* 设置是否填充颜色
* @param isFill
*/
public void setFillColor(boolean isFill){
this.isFillColor = isFill;
invalidate();
}
}
为了颜色设置的灵活性,颜色属性自定义attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomShapTextView">
<!-- Default Paint Color (color). -->
<attr name="paintNormalColor" format="reference|color"/>
<!-- Selected Paint Color (color). -->
<attr name="paintSelectColor" format="reference|color"/>
</declare-styleable>
</resources>
这样子,我们就可以直接在xml中使用自定义的TextView了,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
> <com.example.myapp.CustomShapTextView
android:id="@+id/circle_tv_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@color/blue"
android:text="A"
android:textSize="16sp"
android:textColor="@drawable/tv_view_color"
android:gravity="center"
android:paddingLeft="12dp"
android:paddingTop="12dp"
android:paddingRight="12dp"
android:paddingBottom="12dp"
app:paintNormalColor="@color/gray"
app:paintSelectColor="@color/blue"
/> </RelativeLayout>
现在效果是出来,但是发现圆角虽然画上了,但是文字被覆盖了。
猜测是我们绘制的图层覆盖了文字。
因为自己要画的东西是在下面的,所以要先画上去,然后在让父类(TextView)画他的文字。
所以调整了super.onDraw方法的调用顺序:放到最后调用。
重写TextView,实现圆形背景,文本居中显示的更多相关文章
- CSS文本居中显示
因为一直为元素居中问题而困扰,所以决定把自己遇到和看到的方法记录下来,以便以后查看 如果要让inline或inline-block元素居中显示,则父元素css中包含text-align:center; ...
- 自定义Toast样式-两行文本居中显示
toast可以设置自定义的view和显示位置.下面是一个简单的例子,复杂些的就是改变其布局文件就可以了. /** * @author BMR * @ClassName: ToastWithTwoTex ...
- flex的Accordion组件头部文本居中显示
flex的Accordion组件头部文本默认是居左的,可以通过设置headerStyleName属性使之居中,另外还可以设置字体的样式等 <?xml version="1.0" ...
- ExtJS -- Grid 文本居中显示
Reference: http://stackoverflow.com/questions/6561120/extjs-4-apply-defaults-to-all-columns-in-a-gri ...
- LabelControl文本居中显示
https://www.devexpress.com/Support/Center/Question/Details/Q94915 If you set the AutoSizeMode to Non ...
- CSS样式中,background-image 背景图片居中显示并且在不同屏幕分辨率下始终居中
body { margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; background-colo ...
- css 单行文本居中显示,多行文本左对齐
父级元素 text-align:center; 自级元素 text-align:left; display:inline-block;
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
前言 TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢 ...
- 让TextView的drawableLeft与文本一起居中显示
TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用R ...
随机推荐
- JS滚轮事件(mousewheel/DOMMouseScroll)了解
已经没有了小学生时代过目不忘的记忆力了,很多自己折腾的东西.接触的东西,短短1年之后就全然不记得了.比方说,完全记不得获取元素与页面距离的方法(getBoundingClientRect),或者是不记 ...
- iOS archive(归档)的总结
http://www.cnblogs.com/ios8/p/ios-archive.html
- mysql数据库的连接
public TJb checkjbByschool(long id)throws ClassNotFoundException,SQLException { Class.forName(" ...
- 第八章I/O
一.File的使用 ①.new File(String fileName);的意义 ②.获取当前文件夹下的所有文件 ③.获取当前文件夹时候过滤掉不许要的文件夹 ④.创建File文件,了解mkDir() ...
- Web程序工作原理
1.Web程序工作原理 (1)Web一词的含义 Network:[计算机]电脑网络,网 Web:[计算机]万维网(World Wide Web),互联网(Internet) Web程序,顾名思义,即工 ...
- Java Spring MVC
Spring MVC的实现包括 实现Controller类和基于注解的Controller RequstMapping方式 依赖: <!-- https://mvnrepository.com/ ...
- information_schema.engines学习
当前mysql实例的存储引擎信息可以从information_schema.engines 中查询到 例子: mysql> select * from information_schema.en ...
- c# Parallel并行运算
string str = ""; DataTable dt=new DataTable(); dt.Columns.Add("name", typeof(Sys ...
- js阻止事件冒泡的方法
/********************************************js方法*************************************************** ...
- 微控制器(MCU)破解秘笈--背景知识
2.1 硅芯片安全措施的演变 工业控制器的硬件安全措施与嵌入式系统同时开始发展.三十年前的系统是由分离的部件如CPU,ROM,RAM,I/O缓冲器,串口和其他通信与控制接口组成的.如图2-1所示: 图 ...