让TextView展示不同颜色字体,让局部字体拥有点击事件

发现很多时候展示一堆文字,需要让局部的某些字变粗啊,变大、变颜色、能点击等等要求,今天在这简单总结下方便日后直接复用(用html写的也很简单,在这就不写了),在这里我用 SpannableString 这个类实现下(SpannableStringBuilder 也可以自己看下,大体相似)

String str1 = “欢迎来北京”;

String str2 = "一起看";

String str3 = "太阳";

上面分成三部分,让第二部分实现以下功能;

1、让局部字体变色

SpannableString ss = new SpannableString(str1+ str2 + str6_3 + str3);
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);//红色

ss.setSpan(redSpan,str1.length(),str1.length()+str2.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//sr2的字体变色

TextView content = (TextView) findViewById(R.id.content);

content.append(ss);


2、让局部变色可点击有下划线,俗称超链接

SpannableString ss = new SpannableString(str1+ str2 + str6_3 + str3);
 

ClickableSpan cs1 = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
       //点击事件操作
        avoidHintColor(widget);//设置变色部分字体的背景,不设置的话有一个背景色,在这里我设成透明
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(getResources().getColor(R.color.black_blue));
    }
};
ss1.setSpan(cs1, str1_1.length(), str1_1.length() + str1_2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
content1.append(ss1);
content1.setMovementMethod(LinkMovementMethod.getInstance());//设置超链接


//变色字体部分背景设置成透明得到
private void avoidHintColor(View view) {
    if (view instanceof TextView)
        ((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));
}

3.让局部字体变大、变粗等等看下面可以自己设置啦

AbsoluteSizeSpan(int size) —— 设置字体大小,参数是绝对数值,相当于Word中的字体大小

RelativeSizeSpan(float proportion) —— 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)

ScaleXSpan(float proportion) —— 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)

BackgroundColorSpan(int color) —— 背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)

ForegroundColorSpan(int color) —— 前景着色,也就是字的着色,参数与背景着色一致

TypefaceSpan(String family) —— 字体,参数是字体的名字比如“sans”, “sans-serif”等

StyleSpan(Typeface style) —— 字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。




您可以通过使用 SpannableString 和 ClickableSpan 类来实现TextView 中设置不同字体大小、颜色点击事件的效果。 要设置部分文字的字体大小和颜色,您可以使用 RelativeSizeSpan 和 ForegroundColorSpan。下面是一个示例代码: ```java String text = "这是一个示例文本"; SpannableString spannableString = new SpannableString(text); // 设置部分文字的字体大小 RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.5f); // 放大1.5倍 spannableString.setSpan(sizeSpan, 2, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第2到第5个字符的字体大小 // 设置部分文字的颜色 ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(colorSpan, 8, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第8到第10个字符的颜色 TextView textView = findViewById(R.id.textView); textView.setText(spannableString); ``` 要设置部分文字的点击事件,您可以使用 ClickableSpan。下面是一个示例代码: ```java String text = "点击这里触发事件"; SpannableString spannableString = new SpannableString(text); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // 在这里处理点击事件 Toast.makeText(MainActivity.this, "点击事件触发了", Toast.LENGTH_SHORT).show(); } }; spannableString.setSpan(clickableSpan, 2, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置第2到第4个字符的点击事件 TextView textView = findViewById(R.id.textView); textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); // 必须设置这个方法才能触发点击事件 ``` 上述代码中,我们通过创建 SpannableString 对象,然后使用 setSpan() 方法来设置不同的 Span(包括字体大小、颜色点击事件),最后将 SpannableString 对象设置给 TextView 显示出来。 希望能帮到您!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值