android textView 技巧 富文本显示 代码设置selector(重新整理)

一、富文本显示

1、用xml实现

ex:

appToast.setText(Html.fromHtml("共<font color=yellow>"+ size()+ "</font>个软件,有<font color=yellow>"+Update + "</font>有更新"));

ex:

String parenName =entity.getParent().getMember_nickname();

parantNameRich ="<font color='#ff82ab'>" +parenName +"</font>" ;

holder.parentETV.setText(Html.fromHtml(parantNameRich+": " +parenString));

注: html的解释 是全部一起来的 parantNameRich+ “: ” + parenString

2、用 SpannableString 实现

ex:

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

SpannableString sp = new SpannableString("输入物件名称,例如 \"某某\"选择SG");

// 设置样式一
sp.setSpan(new ForegroundColorSpan(0xff009eff), 10, 14,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// 设置样式二
sp.setSpan(new ForegroundColorSpan(0xff009eff), 16, 18,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

tv.setText(sp);

补充:Android 的html实现TextView的字体大小 用 font size 是无效的。添加这种方法:

1、在string.xml直接实现:用

<string name="item_update"><Data><![CDATA[ <font color=#ffffff >更新至第   </font><font color=#ff0000><big>%1$s</big></font> <font color=#ffffff > 集</font> ]]></Data></string>

java调用:

TextView updateTv = (TextView) v.findViewById(R.id.cardview_update_info);
updateTv.setText(Html.fromHtml(mContext.getResources().getString(R.string.item_update, "" + response.getCurrent())));

2、用 SpannableString 实现

http://www.2cto.com/kf/201112/112871.html 常见的Span有:

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等等。

StrikethroughSpan—-如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样

对于这些Sytle span在使用的时候通常只传上面所说明的构造参数即可,不需要设置其他的属性,如果需要的话,也可以对它们设置其他的属性,详情可以参见文档。

SpannableString和SpannableStringBuilder都有一个设置上述Span的方法:

/**
 * 富文本设置字体大小
 *
 * @param srcString
 * @param tartgetString
 * @param txtSize
 * @return
 */
static public SpannableString setTextPartTextSize(String srcString, String tartgetString, int txtSize) {
    int position = srcString.indexOf(tartgetString);
    SpannableString mSp = new SpannableString(srcString);
    if (position >= 0) {
        mSp.setSpan(new AbsoluteSizeSpan(txtSize, false), position, position + tartgetString.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return mSp;

}


static public SpannableString setTextPartTextSize(String srcString, String tartgetString, int targetColor, int txtSizeDp) {
    int position = srcString.indexOf(tartgetString);
    SpannableString mSp = new SpannableString(srcString);
    if (position >= 0) {
        mSp.setSpan(new AbsoluteSizeSpan(txtSizeDp, true), position, position + tartgetString.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mSp.setSpan(new ForegroundColorSpan(targetColor), position, position + tartgetString.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return mSp;

}

二、用在java代码中设置xml宗的selector 这种用法之前确实是不知道也没有用过 也不是那么简单。今天再看开源代码的时候发现的

ColorStateList csl;
XmlResourceParser xrp = getResources().getXml(R.xml.text_white_blue_selector);
//XmlResourceParser xrp = getResources().getXml(R.drawable.text_white_blue_selector);
try {
   csl = ColorStateList.createFromXml(getResources(), xrp);
} catch (XmlPullParserException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}
tv.setTextColor(csl);

ps: 细心的开发者应该有发现txt.setTextColor 是不能直接设置资源的。因为Color 是int型的,资源id R也是int类型的。
public void setTextColor(ColorStateList colors)
public void setTextColor(int color)

而上面所述的方法就是 setTextColor(ColorStateList colors) 这边来的

ps: 这是早期的代码 编译是可以过去的 但是现在新的gralde工具是编译不过的 (debug可以编译过 release 编译不过) 原因是

XmlResourceParser xrp = getResources().getXml(R.drawable.text_white_blue_selector);
提示 Expected resource of type xml (预期是xml的格式)
解决把这个text_white_blue_selector 选择器文件 移动一个文件夹
在工程Res 目录下新建一个xml 文件夹,把上述文件copy进去。对应句子改成
XmlResourceParser xrp = getResources().getXml(R.xml.text_white_blue_selector);
就ok了

猜你喜欢

转载自blog.csdn.net/lckj686/article/details/79895543