Android 一個TextView中設置多種不同大小的字體,設置超鏈接


以前項目中要是遇到這樣的UI設計,都是傻不拉唧的分為三個TextView來實現,今天在微信中無意中看了一篇公眾號文章,發現原來只要一個TextView就可以搞定啦,人生最悲哀的事情莫過於工作了這么久啦連TextView都沒掌握好,心都涼了,看關鍵代碼吧!!!

1         String text = "這個月您上班遲到了5963次";
2         Spannable textSpan = new SpannableStringBuilder(text);
3         textSpan.setSpan(new AbsoluteSizeSpan(30), 0, text.indexOf("了") + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
4         textSpan.setSpan(new AbsoluteSizeSpan(50), text.indexOf("了") + 1, text.length() - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
5         textSpan.setSpan(new AbsoluteSizeSpan(30), text.length() - 1, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
6         ((TextView) findViewById(R.id.tv)).setText(textSpan);

 普通的跳轉我們都會,不就是一個startActivity(intent)嗎,但是要想在一個TextView中點擊某塊文字實現跳轉呢,那就必須要用到Spannable了,看看如何在TextView中設置超鏈接。

關鍵代碼如下

 1 package com.example.keranbin.testdemo;
 2 
 3 import android.content.Intent;
 4 import android.graphics.Color;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.text.Spannable;
 8 import android.text.SpannableString;
 9 import android.text.SpannableStringBuilder;
10 import android.text.Spanned;
11 import android.text.method.LinkMovementMethod;
12 import android.text.style.AbsoluteSizeSpan;
13 import android.text.style.ClickableSpan;
14 import android.text.style.ForegroundColorSpan;
15 import android.text.style.UnderlineSpan;
16 import android.view.View;
17 import android.widget.TextView;
18 
19 public class MainActivity extends AppCompatActivity {
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         TextView tv = (TextView) findViewById(R.id.tv);
27         //將TextView的顯示文字設置為SpannableString
28         tv.setText(getClickableSpan());
29         //設置該句使文本的超連接起作用
30         tv.setMovementMethod(LinkMovementMethod.getInstance());
31 
32 
33     }
34 
35 
36     //設置超鏈接文字
37     private SpannableString getClickableSpan() {
38         String str ="你想來些水果還是飲料";
39         SpannableString spanStr = new SpannableString(str);
40 
41 
42         //設置下划線文字
43         spanStr.setSpan(new UnderlineSpan(), str.indexOf("水"), str.indexOf("果")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
44         //設置文字的單擊事件
45         spanStr.setSpan(new ClickableSpan() {
46             @Override
47             public void onClick(View widget) {
48                 startActivity(new Intent(MainActivity.this, vegetable.class));
49             }
50         },str.indexOf("水"), str.indexOf("果")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
51         //設置文字的前景色
52         spanStr.setSpan(new ForegroundColorSpan(Color.RED), str.indexOf("水"), str.indexOf("果")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
53         //設置文字的大小
54         spanStr.setSpan(new AbsoluteSizeSpan(50), str.indexOf("水"), str.indexOf("果")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
55 
56 
57         //設置下划線文字
58         spanStr.setSpan(new UnderlineSpan(), str.indexOf("飲"), str.indexOf("料")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
59         //設置文字的單擊事件
60         spanStr.setSpan(new ClickableSpan() {
61             @Override
62             public void onClick(View widget) {
63                 startActivity(new Intent(MainActivity.this, drink.class));
64             }
65         }, str.indexOf("飲"), str.indexOf("料")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
66         //設置文字的前景色
67         spanStr.setSpan(new ForegroundColorSpan(Color.BLUE), str.indexOf("飲"), str.indexOf("料")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
68         //設置文字的大小
69         spanStr.setSpan(new AbsoluteSizeSpan(50), str.indexOf("飲"), str.indexOf("料")+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
70 
71         return spanStr;
72     }
73 
74 }

然后上API查看了下Spannable的setPan()方法,想搞明白第一個參數都可以有些啥,只發現說是Object類型,用來設置字符樣式,段落樣式啥的 ,並沒有過多的介紹。

上網度娘了一下,發現主要有以下屬性可以使用

    1、BackgroundColorSpan 背景色 
    2、ClickableSpan 文本可點擊,有點擊事件
    3、ForegroundColorSpan 文本顏色(前景色)
    4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
    5、MetricAffectingSpan 父類,一般不用
    6、RasterizerSpan 光柵效果
    7、StrikethroughSpan 刪除線(中划線)
    8、SuggestionSpan 相當於占位符
    9、UnderlineSpan 下划線
    10、AbsoluteSizeSpan 絕對大小(文本字體)
    11、DynamicDrawableSpan 設置圖片,基於文本基線或底部對齊。
    12、ImageSpan 圖片
    13、RelativeSizeSpan 相對大小(文本字體)
    14、ReplacementSpan 父類,一般不用
    15、ScaleXSpan 基於x軸縮放
    16、StyleSpan 字體樣式:粗體、斜體等
    17、SubscriptSpan 下標(數學公式會用到)
    18、SuperscriptSpan 上標(數學公式會用到)
    19、TextAppearanceSpan 文本外貌(包括字體、大小、樣式和顏色)
    20、TypefaceSpan 文本字體
    21、URLSpan 文本超鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM