您的位置:首页 > 移动开发 > Android开发

Android控件之TextView学习记录

2016-04-12 23:28 393 查看
在Android中的TextView控件,并不仅仅显示普通文本,仅仅可以设置字体、字号、颜色等属性。

还可以实现超链接、图片显示、部分HTML标记的显示。

实现一:显示一段HTML

String html = "<font color = 'red'>I love Android</font><br/>";
html += "<font color = '#0000ff'><big><i>I love Android</i></big></font><p>";
html += "<big><a href ='http://www.baidu.com'>百度</a></big>";

CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());

String text = "我的URL:http://www.sina.com\n";
text+= "我的email:122798233@qq.com";
text+= "我的电话:168000000000";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());

通过Html.fromHtml方法获取CharSequence接口对象,将文本解析成系统可认的html标记。

实现二:显示图片

textView3.setTextColor(Color.BLACK);
textView3.setBackgroundColor(Color.WHITE);
textView3.setTextSize(20);
String html3="图像1<img src ='image1' />图像2<img src='image2' />图像3<img src='image3' /><p>";
html3 += "图像4<a href='http://www.google.cn'><img src='image4' /></a>图像5<img src='image5' />";
CharSequence charSequence1 =  Html.fromHtml(html3, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(getResourceId(source));
if (source.equals("image3")) {
//处理第三章图片文件的——按照50%的比例压缩
drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);
} else {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
}
}, null);
textView3.setText(charSequence1);
textView3.setMovementMethod(LinkMovementMethod.getInstance());


实现三:使用AutoLink属性实现超链接功能

String html4= "hi百度:";
html4+= "http://www.baidu.com";
textView4.setText(html4);
textView4.setAutoLinkMask(Linkify.ALL);
textView4.setMovementMethod(LinkMovementMethod.getInstance());


实现4:使用SpannableString使用文本的颜色、字号、打开另外Activity、图片显示等功能。

String text1 = "显示打电话Activity1";
String text2 = "显示Activity2";

SpannableString spannableString = new SpannableString(text1);
SpannableString spannableString1 = new SpannableString(text2);
/*spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent(MainActivity.this,Activity1.class);
startActivity(intent);
}
},0,text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);*/
spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new URLSpan("tel:15890003837"), 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

Drawable drawable = getResources().getDrawable(R.drawable.image1, null);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
//spannableString.setSpan(imageSpan,3,1,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


SpannableString类来对指定文本进行相关处理,具体有以下功能:

1、BackgroundColorSpan 背景色

2、ClickableSpan 文本可点击,有点击事件

3、ForegroundColorSpan 文本颜色(前景色)

4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)

5、MetricAffectingSpan 父类,一般不用

6、RasterizerSpan 光栅效果

7、StrikethroughSpan 删除线(中划线)

8、SuggestionSpan 相当于占位符

9、UnderlineSpan 下划线

10、AbsoluteSizeSpan 绝对大小(文本字体)

11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。

12、ImageSpan 图片

13、RelativeSizeSpan 相对大小(文本字体)

14、ReplacementSpan 父类,一般不用

15、ScaleXSpan 基于x轴缩放

16、StyleSpan 字体样式:粗体、斜体等

17、SubscriptSpan 下标(数学公式会用到)

18、SuperscriptSpan 上标(数学公式会用到)

19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)

20、TypefaceSpan 文本字体

21、URLSpan 文本超链接



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: