动态设置TextView边框颜色
来源:互联网 发布:大连东芝医疗知乎 编辑:程序博客网 时间:2024/06/04 18:47
1.在values文件下的attrs.xml中添加样式:
<declare-styleable name="TextViewBorder"> <attr name="tvborderColor" format="color" /> <attr name="tvBorderWidth" format="integer" /></declare-styleable>2.自定义TextView:
public class TextViewBorder extends android.support.v7.widget.AppCompatTextView { private int strokeWidth = 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_tvborderColor, 0);//0 is default strokeWidth=a.getInteger(R.styleable.TextViewBorder_tvborderColor,2); } finally { a.recycle(); } borderPaint = new Paint(); borderPaint.setStyle(Paint.Style.STROKE); borderPaint.setStrokeWidth(strokeWidth); 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(); } public void setBorderWidth(int width){ strokeWidth=width; invalidate(); requestLayout(); }3.布局里使用:
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">4.在调用处可以直接通过TextViewBorder.setBorderColor()赋值。<TextViewBorder android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" app:tvborderColor="@color/green" tools:text="中国武术" /></LinearLayout>
阅读全文
0 0
- 动态设置TextView边框颜色
- Textview动态设置颜色问题
- 动态设置TextView文本颜色。
- Android自定义TextView边框颜色(动态改变边框颜色以及字体颜色)
- Android中动态设置TextView的颜色
- Android中动态设置TextView的颜色
- Activity中动态设置TextView的颜色
- 【Android---项目中】TextView动态设置颜色
- Android之TextView动态设置颜色
- TextView动态设置字体颜色selector不起作用?
- textView设置边框
- 为TextView设置边框
- textView设置边框
- 设置表格边框颜色
- 设置表格边框颜色
- uitextField 设置边框颜色
- 设置textfield边框颜色
- 设置边框颜色
- 随便写写
- mysql插入表中的中文显示为乱码或问号的解决方法
- 由伦敦领军的欧洲顶级数据中心市场蓬勃发展
- session机制详解以及session的相关应用
- MVC,MVP 和 MVVM详解
- 动态设置TextView边框颜色
- hive-2.1.1安装指南
- 阿里巴巴风鸣:做技术Leader要有危机意识
- PhpStorm的使用姿势
- TensorFlow (一) 从入门到实践
- 实现Qt日志功能并输出到文件(qDebug\qWarning\ qCritical\qFatal)
- Unity中通过Editor编程扩展Transform的功能
- 优化ElasticSearch之合理分配索引分片
- 最小二乘法