Android开发 自定义控件 边框TextView
最近在看《Android群英传》里看到的一个自定义效果
亲手把它实现了
在此记录
package csu.lzw.reviewandroid;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;/*** Created by Allen_Binan on 2016/4/1.*/
public class BlockTextView extends TextView {private Paint mInnerPaint;private Paint mOuterPaint;public BlockTextView(Context context) {super(context);}public BlockTextView(Context context, AttributeSet attrs) {super(context, attrs);}public BlockTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {//外层画笔mOuterPaint=new Paint();mOuterPaint.setColor(Color.BLACK);mOuterPaint.setStyle(Paint.Style.FILL);//内层画笔mInnerPaint=new Paint();mInnerPaint.setColor(Color.WHITE);mInnerPaint.setStyle(Paint.Style.FILL);//绘制外层canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mOuterPaint);//绘制内层canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,mInnerPaint);canvas.save();canvas.translate(10, 0);super.onDraw(canvas);canvas.restore();}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><csu.lzw.reviewandroid.BlockTextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:layout_margin="20dp"android:gravity="center"android:text="Hello World!"/>
</LinearLayout>
运行效果
接着又通过自定义属性,将其边框和背景的颜色改为可以在XML布局中自行配制的。
自定义属性
<declare-styleable name="BlockTextView"><attr name="outBlockColor" format="color"/><attr name="InBlockColor" format="color"/></declare-styleable>
界面布局
<?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:orientation="vertical"android:layout_height="match_parent"><csu.lzw.reviewandroid.BlockTextViewandroid:layout_width="match_parent"android:layout_height="80dp"android:layout_margin="20dp"app:outBlockColor="@android:color/holo_red_light"app:InBlockColor="@android:color/holo_blue_bright"android:gravity="center"android:text="Hello World!"/>
</LinearLayout>
package csu.lzw.reviewandroid;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;/*** Created by Allen_Binan on 2016/4/1.*/
public class BlockTextView extends TextView {private Paint mInnerPaint;private Paint mOuterPaint;private int mInnerColor;private int mOuterColor;public BlockTextView(Context context) {super(context);}public BlockTextView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.BlockTextView);mInnerColor=ta.getColor(R.styleable.BlockTextView_InBlockColor,0);mOuterColor=ta.getColor(R.styleable.BlockTextView_outBlockColor,0);ta.recycle();}public BlockTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {//外层画笔mOuterPaint=new Paint();mOuterPaint.setColor(mOuterColor);mOuterPaint.setStyle(Paint.Style.FILL);//内层画笔mInnerPaint=new Paint();mInnerPaint.setColor(mInnerColor);mInnerPaint.setStyle(Paint.Style.FILL);//绘制外层canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mOuterPaint);//绘制内层canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,mInnerPaint);canvas.save();canvas.translate(10, 0);super.onDraw(canvas);canvas.restore();}
}
然后的运行效果
Android开发 自定义控件 边框TextView相关推荐
- android按钮设置下划线,Android开发如何给textView设置下划线或中划线
关键字:android,android开发,下划线,中划线 我们在开发应用的时候,尤其是在做商城项目的时候,需要用到原价格以及降价后的价格,这就不可避免用到中划线或者下划线,在原价格上做一个中划线,这 ...
- [Android开发]镂空的TextView,镂空字体,TextView实现镂空字体效果
前言:本来是下班途中跟IOS的同事一起走在路上闲聊,他让我看了一个App的镂空字体效果,然后我俩讨论了几句,我当时已经有一个大体上的实现思路了,于是第二天就实现出来了,讲真的目前这玩意没有用到我开发中 ...
- Android开发技巧——去掉TextView中autolink的下划线
我们知道,在布局文件中设置textview的autolink及其类型,这时textivew上会显示link的颜色,并且文字下面会有一条下划线,表示可以点击.而在我们在点击textview时,应用将根据 ...
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
前言 TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢 ...
- android 地图面积测量,Android开发自定义控件,实现Arcgis for Android测距、测面积功能...
采用Arcgis Runtime for Android 100.3.0开发. 控件的功能包括,测距.测面积.撤销.恢复.清除.完成六个功能. 测距:在地图上绘制线段进行长度测量 测面积:在地图上绘制 ...
- autolink android不显示下划线,Android开发技巧——去掉TextView中autolink的下划线
我们知道,在布局文件中设置textview的autolink及其类型,这时textivew上会显示link的颜色,并且文字下面会有一条下划线,表示可以点击.而在我们在点击textview时,应用将根据 ...
- android 动态 wrap_content,Android开发自定义控件View应对wrap_content属性处理
直接修改onMeasure()方法即可 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) ...
- Android开发技巧——自定义控件之自定义属性
Android开发技巧--自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码. 上一篇讲了如何通过xml把几个控件组织起来,并继承某个 ...
- android自定义省略号,Android开发自定义TextView省略号样式的方法
本文实例讲述了Android开发自定义TextView省略号样式的方法.分享给大家供大家参考,具体如下: 在布局xml中设置textView的字段 android:maxLines="2&q ...
最新文章
- 【转】Linux Oracle服务启动停止脚本与开机自启动
- tinyxml 读取文本节点_TinyXml 读写XML文件
- 智能卡技术和身份认证
- python教学视频-Python入门视频课程
- 文献学习(part40)--Efficient multi-modal geometric mean metric learning
- arcgis导入excel字段不显示_ArcGIS从excel中导入坐标出现空白无法选择字段
- Careercup - Microsoft面试题 - 5752271719628800
- Lucene的索引链结构_IndexChain
- C# 类型转换问题一
- 复习-arrary和arraylist的对比以及arraylist的遍历中删除的原理
- html颜色代码生成器,在线取色配色工具,CSS3渐变色代码生成器-易玩稀有
- JavaScript 原生js实现自定义消息提示框
- oracle只有dbf文件能不能恢复,Oracle 误删DBF文件恢复
- C51单片机实现 贪吃蛇
- 【信奥题库 NOIP 2020 在线模拟赛 T1】有趣的函数
- 网络Socket编程
- flash在线视频播放器
- MATLAB之傅里叶展开(五)
- 电脑速度慢的一些方法
- ThinkCMF图片上传