SpannableString(图文混排)

package com.gxb.spannedstring;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.URLSpan;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.tv);

        String txt = "快来看";
        SpannableString span = new SpannableString(txt);
        //背景颜色
//        span.setSpan(new BackgroundColorSpan(Color.parseColor("#00ff00")), 0, txt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


        //字体颜色
//        span.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")),0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


        //删除线
//        span.setSpan(new StrikethroughSpan(),0,span.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置图片1
        /*Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);

        //设置一个边界
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        span.setSpan(new ImageSpan(drawable),
                0, 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);*/

        //设置图片2
       /* Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        drawable.setBounds(0,0,drawable.getIntrinsicHeight(),drawable.getIntrinsicWidth());
        span.setSpan(new ImageSpan(drawable),0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);*/


        //设置图片3
        // DynamicDrawableSpan.ALIGN_BASELINE表示依照基线对齐
        // DynamicDrawableSpan.ALIGN_BOTTOM表示依照底部对齐
        /*span.setSpan(new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {
            @Override
            public Drawable getDrawable() {
                Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
                d.setBounds(0,0,45,45);
                return d;
            }
        },0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);*/

        //设置超链接
//        span.setSpan(new URLSpan("http://www.baidu.com"),0,span.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //打电话
//        span.setSpan(new URLSpan("tel:123456789"),0,span.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //发短信
//        span.setSpan(new URLSpan("smsto:123456789"),0,span.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


        //发邮件
//        span.setSpan(new URLSpan("mailto:[email protected]"),0,span.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
       tv.setText(span);

        //设置超链接必须添加的方法
        tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

你可能感兴趣的:(SpannableString(图文混排))