你好,游客 登录 注册 搜索
背景:
阅读新闻

Android TextView设置字体风格

[日期:2011-10-13] 来源:Linux社区  作者:hgl868 [字体:大 中 小]
Linkify


另外,也可以对通过TextView.setAutoLink(int)设置其Linkify属性,其用处在于,TextView会自动检查其内容,会识别出phone number, web address or email address,并标识为超链接,可点击,点击后便跳转到相应的应用,如Dialer,Browser或Email。Linkify有几个常用选项:

Linkify.EMAIL_ADDRESS -- 仅识别出TextView中的Email在址,标识为超链接,点击后会跳到Email,发送邮件给此地址

Linkify.PHONE_NUMBERS -- 仅识别出TextView中的电话号码,标识为超链接,点击后会跳到Dialer,Call这个号码
Linkify.WEB_URLS-- 仅识别出TextView中的网址,标识为超链接,点击后会跳到Browser打开此URL
Linkify.ALL -- 这个选项是识别出所有系统所支持的特殊Uri,然后做相应的操作
权衡选择
个人认为软件开发中最常见的问题不是某个技巧怎么使用的问题,而是何时该使用何技巧的问题,因为实现同一个目标可能有N种不同的方法,就要权衡利弊,选择最合适的一个,正如常言所云,没有最好的,只有最适合的。如前面所讨论的,要想用不同的字体展现不同的信息可能的解法,除了用Style Span外还可以用多个TextView。那么就需要总结下什么时候该使用StyleSpan,什么时候该使用多个TextView:

如果显示的是多个不同类别的信息,就应该使用多个TextView,这样也方便控制和改变各自的信息,例子就是默认LockScreen上面的日期和充电信息,因为它们所承载不同的信息,所以应该使用多个TextView来分别呈现。
如果显示的是同一类信息,或者同一个信息,那么应该使用StyleSpan。比如,短信息中,要把联系人的相关信息突出显示;或是想要Highlight某些信息等。
如果要实现Rich text,没办法,只能使用Style span。
如果要实现某些特效,也可以考虑使用StyleSpan。设置不同的字体风格只是Style span的初级应用,如果深入研究,可以发现很多奇妙的功效。
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
      xmlns:Android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <ScrollView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
            <LinearLayout 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:orientation="vertical"> 
                  <TextView 
                    android:id="@+id/text_view_font_1" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    /> 
                  <TextView 
                    android:id="@+id/text_view_font_2" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    /> 
                  <TextView 
                    android:id="@+id/text_view_font_3" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    /> 
                  <TextView 
                    android:id="@+id/text_view_font_4" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    /> 
                  <TextView 
                    android:id="@+id/text_view_font_5" 
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" 
                    /> 
           </LinearLayout> 
        </ScrollView> 
    </LinearLayout> 

 


    package com.android.effective; 

    import java.util.regex.Matcher; 
    import java.util.regex.Pattern; 
    import android.app.Activity; 
    import android.graphics.Color; 
    import android.graphics.Typeface; 
    import android.os.Bundle; 
    import android.text.Spannable; 
    import android.text.SpannableString; 
    import android.text.SpannableStringBuilder; 
    import android.text.style.AbsoluteSizeSpan; 
    import android.text.style.BackgroundColorSpan; 
    import android.text.style.ForegroundColorSpan; 
    import android.text.style.QuoteSpan; 
    import android.text.style.RelativeSizeSpan; 
    import android.text.style.ScaleXSpan; 
    import android.text.style.StrikethroughSpan; 
    import android.text.style.StyleSpan; 
    import android.text.style.TypefaceSpan; 
    import android.text.style.URLSpan; 
    import android.text.util.Linkify; 
    import android.widget.TextView; 
     
    public class TextViewFontActivity extends Activity { 
     
        @Override 
     
        public void onCreate(Bundle bundle) { 
     
            super.onCreate(bundle); 
     
            setContentView(R.layout.textview_font_1); 
 
            // Demonstration of basic SpannableString and spans usage 
     
            final TextView textWithString = (TextView) findViewById(R.id.text_view_font_1); 
     
            String w = "The quick fox jumps over the lazy dog"; 
     
            int start = w.indexOf('q'); 
     
            int end = w.indexOf('k') + 1; 
     
            Spannable word = new SpannableString(w); 
     
            word.setSpan(new AbsoluteSizeSpan(22), start, end,  
     
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word.setSpan(new StyleSpan(Typeface.BOLD), start, end,  
     
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word.setSpan(new BackgroundColorSpan(Color.RED), start, end,  
     
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            textWithString.setText(word); 
     
            // Demonstration of basic SpannableStringBuilder and spans usage 
     
            final TextView textWithBuilder = (TextView) findViewById(R.id.text_view_font_2); 
     
            SpannableStringBuilder word2 = new SpannableStringBuilder(); 
     
            final String one = "Freedom is nothing but a chance to be better!"; 
     
            final String two = "The quick fox jumps over the lazy dog!"; 
     
            final String three = "The tree of liberty must be refreshed from time to time with " + 
     
                    "the blood of patroits and tyrants!"; 
     
            word2.append(one); 
     
            start = 0; 
     
            end = one.length(); 
     
            word2.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     
            word2.append(two); 
     
            start = end; 
     
            end += two.length(); 
     
            word2.setSpan(new ForegroundColorSpan(Color.CYAN), start, end,  
     
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     
            word2.append(three); 
     
            start = end; 
     
            end += three.length(); 
     
            word2.setSpan(new URLSpan(three), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     
            textWithBuilder.setText(word2); 
     
            // Troubleshooting when using SpannableStringBuilder 
     
            final TextView textTroubles = (TextView) findViewById(R.id.text_view_font_3); 
     
            SpannableStringBuilder word3 = new SpannableStringBuilder(); 
     
            start = 0; 
     
            end = one.length(); 
     
            // Caution: must first append or set text to SpannableStringBuilder or SpannableString 
     
            // then set the spans to them, otherwise, IndexOutOfBoundException is thrown when setting spans 
     
            word3.append(one); 
     
            // For AbsoluteSizeSpan, the flag must be set to 0, otherwise, it will apply this span to until end of text 
     
            word3.setSpan(new AbsoluteSizeSpan(22), start, end, 0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            // For BackgroundColorSpanSpan, the flag must be set to 0, otherwise, it will apply this span to end of text 
     
            word3.setSpan(new BackgroundColorSpan(Color.DKGRAY), start, end, 0); //Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.append(two); 
     
            start = end; 
     
            end += two.length(); 
     
            word3.setSpan(new TypefaceSpan("sans-serif"), start, end,  
     
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            // TODO: sometimes, flag must be set to 0, otherwise it will apply the span to until end of text 
     
            // which MIGHT has nothing to do with specific span type. 
     
            word3.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, 0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.setSpan(new ScaleXSpan(0.618f), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.setSpan(new StrikethroughSpan(), start, end, 0);//Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.setSpan(new ForegroundColorSpan(Color.CYAN), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.setSpan(new QuoteSpan(), start, end, 0); //Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.append(three); 
     
            start = end; 
     
            end += three.length(); 
     
            word3.setSpan(new RelativeSizeSpan((float) Math.E), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
            word3.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

            textTroubles.setText(word3); 
     
            // Highlight some patterns 
     
            final String four = "The gap between the best software engineering " + 
     
                    "practice and the average practice is very wide?aperhaps wider " + 
     
                    " than in any other engineering discipline. A tool that disseminates " + 
     
                    "good practice would be important.?aFred Brooks"; 
     
            final Pattern highlight = Pattern.compile("the"); 
     
            final TextView textHighlight = (TextView) findViewById(R.id.text_view_font_4); 
     
            SpannableString word4 = new SpannableString(four); 
     
            Matcher m = highlight.matcher(word4.toString()); 
     
            while (m.find()) { 
     
                word4.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), m.start(), m.end(),  
     
                        Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
                word4.setSpan(new ForegroundColorSpan(Color.RED), m.start(), m.end(),  
     
                        Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     
                word4.setSpan(new StrikethroughSpan(), m.start(), m.end(),  
     
                        Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
            } 
     
            textHighlight.setText(word4); 

            // Set numbers, URLs and E-mail address to be clickable with TextView#setAutoLinkMask 
     
            final TextView textClickable = (TextView) findViewById(R.id.text_view_font_5);   
     
            final String contact = "Email: mvp@microsoft.com\n" + 
     
                    "Phone: +47-24885883\n" + 
     
                    "Fax: +47-24885883\n" + 
     
                    "HTTP: www.microsoft.com/mvp.asp"; 
     
            // Set the attribute first, then set the text. Otherwise, it won't work 
     
            textClickable.setAutoLinkMask(Linkify.ALL); // or set 'android:autoLink' in layout xml 
     
            textClickable.setText(contact); 
        } 
    } 

linux
相关资讯       Android基础教程  Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款