自定义支持圆角的TextView
来源:互联网 发布:网络机柜出厂检验报告 编辑:程序博客网 时间:2024/06/15 16:05
给textview添加圆角
如果想给一个普通的textview添加圆角、边框等,一般的做法是写一个drawable
文件,通过android:background="@drawable/xxxx"
设置为textview的背景。麻烦是不麻烦,可是如果项目里出现了很多需要圆角或者边框的需求时,drawable
文件会变得很多很乱,维护起来也十分不方便。
如果直接可以通过属性设置radius
border
borderWidth
等属性值,就会方便很多。github上有一个 SuperTextView ,可以实现。但是功能太多太杂了。搜了搜网上的写法,基本都是重新定义view,重写onDraw
onMeasure
,可是这些功能textview都有,没必要,况且重写之后,textview的很多功能就没了。
这里的思路是,通过继承TextView来自定义Textview,利用代码来控制drawable
文件。代码创建drawable文件的方式如下:
自定义view的方式有三种:组合、继承、完全自定义
GradientDrawable gd = new GradientDrawable();//创建drawablegd.setColor(rtvBgColor);gd.setCornerRadius(rtvRadius);
所以方法就是,自定义属性,通过属性值创建drawable
文件控制圆角、边框等。如果不设置自定义属性,和一个普通TextView没有任何差别!
代码
/** * 支持圆角的TextView * Created by stephen on 2017/12/18. */public class RoundTextView extends android.support.v7.widget.AppCompatTextView { public RoundTextView(Context context) { this(context, null); } public RoundTextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RoundTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundTextView, defStyleAttr, 0); if (attributes != null) { int rtvBorderWidth = attributes.getDimensionPixelSize(R.styleable.RoundTextView_rtvBorderWidth, 0); int rtvBorderColor = attributes.getColor(R.styleable.RoundTextView_rtvBorderColor, Color.BLACK); float rtvRadius = attributes.getDimension(R.styleable.RoundTextView_rtvRadius, 0); int rtvBgColor = attributes.getColor(R.styleable.RoundTextView_rtvBgColor, Color.WHITE); attributes.recycle(); GradientDrawable gd = new GradientDrawable();//创建drawable gd.setColor(rtvBgColor); gd.setCornerRadius(rtvRadius); if (rtvBorderWidth > 0) { gd.setStroke(rtvBorderWidth, rtvBorderColor); } this.setBackground(gd); } } public void setBackgroungColor(@ColorInt int color) { GradientDrawable myGrad = (GradientDrawable) getBackground(); myGrad.setColor(color); }}
在attr
中添加属性
<!--支持圆角的TextView--><declare-styleable name="RoundTextView"> <attr name="rtvBgColor" format="color"/> <attr name="rtvBorderWidth" format="dimension"/> <attr name="rtvBorderColor" format="dimension"/> <attr name="rtvRadius" format="dimension"/></declare-styleable>
代码使用
<io.github.imwyy.RoundTextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="浏览" android:textColor="@color/text_black_select_color" android:textSize="@dimen/sp_14" app:rtvRadius="6dp" app:rtvBgColor="@color/colorSearchArea"/>
这样显然就方便很多了。
阅读全文
0 0
- 自定义支持圆角的TextView
- 自定义圆角TextView
- android TextView 支持自定义字体和属性
- TextView 支持自定义字体和属性
- textview的自定义实现
- TextView的自定义字体
- 自定义闪烁的TextView
- 自定义的TextView滚动
- 自定义的textView
- 自定义圆角点击变色TextView
- 自定义圆角TextView 背景色
- TextView支持的Html标签
- TextView支持的XML属性
- 支持展开的TextView----ExpandTextView
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- Android自定义控件之《自定义TextView(支持显示图片)》
- mysql解决中文乱码问题
- 程序员不适合创业
- web静态服务器-非阻塞模式
- 死磕红皮书(基本概念)
- js 的闭包和构造函数
- 自定义支持圆角的TextView
- Python之列表,元组,字符串(4)
- 2017 CCPC 秦皇岛 & ZOJ 3987
- Java Base64编码使用介绍
- Android网络加载图片
- Capstone训练营第十天
- kindeditor文本编辑器配置和使用方法
- Java中8种基本数据类型及其默认值
- python基础第三天