SpannableStringBuilder使用

package com.idt.os.frameplay;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.text.style.ScaleXSpan;
import android.text.style.StyleSpan;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



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

        final SpannableStringBuilder ssb = new SpannableStringBuilder();
        final int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
        int start;
        int end;
        ssb.append("This text is normal,but ");
        start = ssb.length();
        ssb.append("this text is bold");
        end = ssb.length();
        ssb.setSpan(new StyleSpan(Typeface.BOLD),start,end,flag); //加粗文本

        ssb.append('\n');
        start = end++;
        ssb.append('\uFFFC');    // Unicode替换字符
        end = ssb.length();
        ssb.setSpan(new ImageSpan(this,R.mipmap.ic_launcher), //设置图片
                start,end,flag);
        ssb.append('\n');
        ssb.append("This text is wide");
        end = ssb.length();
        ssb.setSpan(new ScaleXSpan(2f),start,end,flag);   //拉伸文本
        final TextView tv = new TextView(this);
        tv.setText(ssb);
        setContentView(tv);
    }

}

你可能感兴趣的:(Anroid学习笔记)