package hyz.com.font;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.ComponentName;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;
public class FontStyleTestActivity extends Activity
{
String strs="我的心太乱了,给我点空白。";
TextView tv,tv1,tv2,tv3,tv4,tv5;
int start =3;
int end = 5;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
tv4=(TextView)findViewById(R.id.tv4);
tv5=(TextView)findViewById(R.id.tv5);
method1();
method2();
method3();
method4();
method5();
}
//设置部分文字背景高亮显示:
private void method1()
{
SpannableStringBuilder style=new SpannableStringBuilder(strs);
style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv1.setText(style);
}
//同时设置文字和背景高亮显示:
private void method2()
{
SpannableStringBuilder style=new SpannableStringBuilder(strs);
style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv2.setText(style);
}
private void method3()
{
SpannableStringBuilder style=new SpannableStringBuilder(strs);
style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv3.setText(style);
}
/*
* Spannable.SPAN_EXCLUSIVE_EXCLUSIVE :
* 即在原文本头或尾追加新文本的样式不受原文本样式影响,原文本高亮,新追加文本不高亮
* Spannable.SPAN_EXCLUSIVE_INCLUSIVE :
* 即在原文本尾追加新文本的样式受原文本样式影响,原来文本尾高亮,新追加文本也高亮
*/
private void method4()
{
tv4.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));
}
private void method5()
{
SpannableString ss = new SpannableString( "红色打电话粗体删除线绿色下划线图片:.");
SpannableString ss1 = new SpannableString( "红色打电话粗体删除线绿色下划线图片:.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//粗斜体
ss.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new StrikethroughSpan(), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 10, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
// d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
// ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
// ss.setSpan(span, 18, 19, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// ss.setSpan(new AbsoluteSizeSpan(18), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Map<String , Object> map = new HashMap<String , Object>();
map.put("key", ss);
tv5.setText((SpannableString)map.get("key"));
tv5.setMovementMethod(LinkMovementMethod.getInstance());
}
}