首页 > Android > 正文

android textview 设置不同的字体大小和颜色

2016-07-18 Android 3273 ℃ 0 评论

在开发应用过程中经常会遇到显示一些不同的字体风格的信息犹如默认的LockScreen上面的时间和充电信息。对于类似的情况,可能第一反应就是用不同的多个TextView来实现,对于每个TextView设置不同的字体风格以满足需求。

这里推荐的做法是使用Android.text.*;和android.text.style.*;下面的组件来实现RichText:也即在同一个TextView中设置不同的字体风格。对于某些应用,比如文本编辑,记事本,彩信,短信等地方,还必须使用这些组件才能达到想到的显示效果。

主要的基本工具类有Android.text.Spanned; android.text.SpannableString; android.text.SpannableStringBuilder;使用这些类来代替常规String。SpannableString和SpannableStringBuilder可以用来设置不同的Span,这些Span便是用于实现Rich Text,比如粗体,斜体,前景色,背景色,字体大小,字体风格等等,android.text.style.*中定义了很多的Span类型可供使用。


因为Spannable等最终都实现了CharSequence接口,所以可以直接把SpannableString和SpannableStringBuilder通过TextView.setText()设置给TextView。

使用方法
当要显示Rich Text信息的时候,可以使用创建一个SpannableString或SpannableStringBuilder,它们的区别在于SpannableString像一个String一样,构造对象的时候传入一个String,之后再无法更改String的内容,也无法拼接多个SpannableString;而SpannableStringBuilder则更像是StringBuilder,它可以通过其append()方法来拼接多个String:

SpannableString word = new SpannableString("The quick fox jumps over the lazy dog");  
  
SpannableStringBuilder multiWord = new SpannableStringBuilder();  
multiWord.append("The Quick Fox");  
multiWord.append("jumps over");  
multiWord.append("the lazy dog");


创建完Spannable对象后,就可以为它们设置Span来实现想要的Rich Text了,常见的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.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。
StrikethroughSpan----如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样
对于这些Sytle span在使用的时候通常只传上面所说明的构造参数即可,不需要设置其他的属性,如果需要的话,也可以对它们设置其他的属性。
SpannableString和SpannableStringBuilder都有一个设置上述Span的方法:

    /** 
     * Set the style span to Spannable, such as SpannableString or SpannableStringBuilder 
     * @param what --- the style span, such as StyleSpan 
     * @param start --- the starting index of characters to which the style span to apply 
     * @param end --- the ending index of characters to which the style span to apply 
     * @param flags --- the flag specified to control 
     */  
    setSpan(Object what, int start, int end, int flags);

 

其中参数what是要设置的Style span,start和end则是标识String中Span的起始位置,而 flags是用于控制行为的,通常设置为0或Spanned中定义的常量,常用的有:

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点

Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点
Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点
Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点
这里理解起来就好像数学中定义区间,开区间还是闭区间一样的。这里要重点说明下关于参数0,有很多时候,如果设置了上述的参数,那么Span会从start应用到Text结尾,而不是在start和end二者之间,这个时候就需要使用Flag 0。


单例


步骤如下:

      1.定义不同style .

<style name="dialog_bt_style0">
    <item name="android:textSize">15dp</item>
    <item name="android:textColor">#289eff</item>
</style>

<style name="dialog_bt_style1">
    <item name="android:textSize">12dip</item>
    <item name="android:textColor">#999999</item>
</style>

2 . 通过SpannableString 设置字符串格式。代码如下:

public MyManageAlertDialog setPositiveButton(String text,String value,final View.OnClickListener listener) {
    SpannableString styledText;
    if(StringUtil.isNulls(value)){
        styledText = new SpannableString(text+"\n"+value);
    }else{
        styledText = new SpannableString(text);
    }
    styledText.setSpan(new TextAppearanceSpan(context, R.style.dialog_bt_style0), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if(StringUtil.isNulls(value)) {
        styledText.setSpan(new TextAppearanceSpan(context, R.style.dialog_bt_style1), text.length(), text.length()+value.length()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    btn_pos.setText(styledText, TextView.BufferType.SPANNABLE);
    btn_pos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != listener)
                listener.onClick(v);
            dialog.dismiss();
        }
    });
    return this;
}


猜你喜欢

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接