TextView之你不知道的那些事兒

下面就是代碼(註釋很詳細)

public class MainActivity extends AppCompatActivity {

    private TextView tv1, tv2, tv3, tv4, tv5, tv6, tv8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv1.setText(getString(R.string.xliff_1, "code小生", "Android愛好者"));

        tv2 = (TextView) findViewById(R.id.tv2);
        tv2.setTypeface(Typeface.createFromAsset(getAssets(), "simkai.ttf"));

        tv3 = (TextView) findViewById(R.id.tv3);
        setTextSpannable(tv3);

        // 定製超鏈接
        tv4 = (TextView) findViewById(R.id.tv4);
        tv4.setText(getClickableSpan(tv4));
        //設置該句使設置了鏈接的文本的超連接起作用
        tv4.setMovementMethod(LinkMovementMethod.getInstance());

        // 設置多種顏色字體
        tv5 = (TextView) findViewById(R.id.tv5);
        String str1 = "<font color=\"#404f50\">年輕的我們,</font>";
        String str2 = "<font color=\"#ff0000\">即將奔赴戰場</font>";
        tv5.setText(Html.fromHtml(str1 + str2));

        // 加載html中的圖片
        tv6 = (TextView) findViewById(R.id.tv6);
        setHtmlBitmap(tv6);

        tv8 = (TextView) findViewById(R.id.tv8);
        String text = String.format("¥%1$s 門市價:¥%2$s", "88.8", "150");
        int index = text.indexOf("門");
        SpannableStringBuilder span = new SpannableStringBuilder(text);
        span.setSpan(new ForegroundColorSpan(Color.parseColor("#00ff00")), 0, index - 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        span.setSpan(new AbsoluteSizeSpan(28), index, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.parseColor("#afafaf")), index, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        tv8.setText(span);
    }
    //http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0120/2335.html

    /**
     *
     * 設置TextView中的某個詞突出顯示
     *
     * @param textView
     */
    private void setTextSpannable(TextView textView) {
        String text = textView.getText().toString().trim();
        int start = text.indexOf('5');
        int end = text.length();
        Spannable textSpan = new SpannableStringBuilder(text);
        textSpan.setSpan(new AbsoluteSizeSpan(30), 0, start, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        textSpan.setSpan(new AbsoluteSizeSpan(45), start, end - 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        // 設置文字顏色
        textSpan.setSpan(new ForegroundColorSpan(Color.BLUE), start, end - 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new AbsoluteSizeSpan(30), end - 3, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(textSpan);
    }

    //給指定文本設置超鏈接
    private SpannableString getClickableSpan(TextView textView) {
        String text = textView.getText().toString().trim();
        final SpannableString spanStr = new SpannableString(text);
        spanStr.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //設置文字的單擊事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "服務條款", Toast.LENGTH_SHORT).show();
            }
        }, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 取消點擊時的默認背景色
        spanStr.setSpan(new BackgroundColorSpan(Color.WHITE), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spanStr.setSpan(new UnderlineSpan(), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //設置文字的單擊事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "隱私政策", Toast.LENGTH_SHORT).show();
            }
        }, 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new ForegroundColorSpan(Color.GREEN), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new BackgroundColorSpan(Color.WHITE), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spanStr;
    }

    /**
     * TextView加載html/xml中的圖片和文字
     *
     * @param textView
     */
    private void setHtmlBitmap(TextView textView) {
        String imgStr = "<b>this is html text</b><br><img src=\"" + R.mipmap.ic_launcher + "\"/>";
        Html.ImageGetter imageGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                int id = Integer.parseInt(source);
                Drawable draw = getResources().getDrawable(id);
                draw.setBounds(0, 0, 200, 200);
                return draw;
            }
        };
        textView.append(Html.fromHtml(imgStr, imageGetter, null));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章