android TextView 设置部分文本 边框和样式
16lz
2022-04-21
1 继承之后在绘制函数中处理
package com.test.withborderstextview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.widget.TextView;/** * Created by lp on 2016/9/21. */public class TextViewBorder extends TextView { private static final int STROKE_WIDTH = 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_borderColor, 0);//0 is default } finally { a.recycle(); } borderPaint = new Paint(); borderPaint.setStyle(Paint.Style.STROKE); borderPaint.setStrokeWidth(STROKE_WIDTH); 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(); }}
其中的style在style.xml中有定义
<declare-styleable name="TextViewBorder"> <attr name="borderColor" format="color"/> declare-styleable>
接下来就看看怎么引用:这里需要注意引入 xmlns:app=”http://schemas.android.com/apk/res-auto”
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.test.withborderstextview.TextViewBorder android:id="@+id/state1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="3dp" android:layout_margin="10dp" android:text="状态1(xml设置)" android:textColor="@color/app_blue_color" app:borderColor="@color/app_blue_color" /> <com.test.withborderstextview.TextViewBorder android:id="@+id/state2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="3dp" android:layout_margin="10dp" android:text="状态2(java代码动态更改)" android:textColor="@color/app_blue_color" app:borderColor="@color/app_blue_color" /> <com.test.withborderstextview.TextViewBorder android:id="@+id/state3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="3dp" android:layout_margin="10dp" android:text="状态3(java代码动态更改)" android:textColor="@color/app_blue_color" app:borderColor="@color/app_blue_color" /> <com.test.withborderstextview.TextViewBorder android:id="@+id/state4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:layout_margin="10dp" android:padding="3dp" android:text="状态4(java代码动态更改)" android:textColor="@color/app_blue_color" app:borderColor="@color/app_blue_color" />
最后我们来看看怎么在代码中动态更改边框以及字体颜色
MainActivity.java
package com.test.withborderstextview;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { private TextViewBorder state1; private TextViewBorder state2; private TextViewBorder state3; private TextViewBorder state4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); state1 = (TextViewBorder) findViewById(R.id.state1); state2 = (TextViewBorder) findViewById(R.id.state2); state3 = (TextViewBorder) findViewById(R.id.state3); state4 = (TextViewBorder) findViewById(R.id.state4); //边框颜色 state2.setBorderColor(getResources().getColor(R.color.app_red_delete_color)); //字体颜色 state2.setTextColor(getResources().getColor(R.color.app_red_delete_color)); state3.setBorderColor(getResources().getColor(R.color.app_blue_color)); state3.setTextColor(getResources().getColor(R.color.progress_color)); state4.setBorderColor(getResources().getColor(R.color.app_red_delete_color)); state4.setTextColor(getResources().getColor(R.color.app_blue_color)); }}
2 直接通过SpannableString 绘制修改
SpannableString msp = new SpannableString("默认"); Drawable bg = context.getResources().getDrawable(R.drawable.default_address_tv); bg.setBounds(0, (int) Utils.applyDimension(3, -7, context), (int) Utils.applyDimension(3, msp.length() + 34, context), (int) Utils.applyDimension(3, 10, context)); msp.setSpan(new ImageSpan(bg) { @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { int len = Math.round(paint.measureText(text, start, end)); getDrawable().setBounds(0, (int) Utils.applyDimension(3, -7, context), (int) Utils.applyDimension(3, msp.length() + 34, context), (int) Utils.applyDimension(3, 10, context)); super.draw(canvas, text, start, end, x, top, y, bottom, paint); paint.setColor(context.getResources().getColor(R.color.color_7dbffa));// paint.setTypeface(Typeface.create("normal", Typeface.BOLD)); paint.setTextSize(Utils.applyDimension(3, 12, context)); canvas.drawText(text.subSequence(start, end).toString(), Utils.applyDimension(3, x + 6, context), y, paint); // 绘制文本 } }, 0, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holder.tvAddress.setText(msp); SpannableString msp2 = new SpannableString(" " + address); holder.tvAddress.append(msp2);
更多相关文章
- Android沉浸式通知栏的一个开源库SystemBarTint简单使用,android
- 【Android】Android(安卓)permission 访问权限
- Android关于图片处理,更改图片灰度
- Android检测电源状态
- android camera系统3A模式及其状态转换(二)
- Android监听屏幕屏幕锁屏与解锁
- android中去掉标题栏和状态栏
- Android启动画面Splash
- Android: 判断网络状态