Android中SpannableString的用法

2011-12-14

废话少说,具体代码示例如下:

package net.chengyong.ss;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView = (TextView) findViewById(R.id.textview);
        SpannableString ss = new SpannableString("abcdefghijklmn");
        Drawable d = getResources().getDrawable(R.drawable.icon);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, 0, 6, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(ss);

    }
}

无回应:“Android中SpannableString的用法”

发表评论

电子邮件地址不会被公开。 必填项已用*标注