没事干总结了一些 平常经常需要用到的 文件显示的东西 一些东西,后面还会陆续补充的 - -
TextView text0= (TextView) this.findViewById(R.id.text0);
TextView text1= (TextView) this.findViewById(R.id.text1);
TextView text2= (TextView) this.findViewById(R.id.text2);
TextView text3= (TextView) this.findViewById(R.id.text3);
TextView text4= (TextView) this.findViewById(R.id.text4);
EditText edittext5= (EditText) this.findViewById(R.id.text5);
TextView text6= (TextView) this.findViewById(R.id.text6);
TextView text7= (TextView) this.findViewById(R.id.text7);
String s ="甘家寨的8排裤带面太好吃了,我和我的小伙伴们惊呆了!";
// u-underline i -italic b - bold
text0.setText(Html.fromHtml("<u>"+s+"</u>")); //下面有下标
text1.setText(Html.fromHtml("<b>"+s+"</b>")); //粗体
text2.setText(Html.fromHtml("<i>"+s+"</i>")); //斜体
text3.setText(Html.fromHtml("<a href=\'http://www.google.com\'>"+ s +"</a>" )); //超连接
//如果要在 Strings 里面直接设置的话 需要转义
//例如
//<resources>
//<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
//</resources>
//因为fromHtml(String)方法会格式化所有的HTML内容,所以要确保用htmlEncode(String)对带格式化文本的字符串内所有可能的HTML字符进行转义。
//比如,如果要把可能包含诸如“<”或“&”等字符的串作为参数传给String.format(),那么必须在格式化之前对这些字符进行转义。格式化后,再把字符串传入fromHtml(String),这些特殊字符就能还原成本来意义了。例如:
//String escapedUsername = TextUtil.htmlEncode(username)
//Resources res = getResources();
//String text = String.format(res.getString(R.string.welcome_messages), escapedUsername,mailCount);
//CharSequence styledText = Html.fromHtml(text);
/**
* *********************** text4 text 5 ***************************************
*/
//Spannable -(翻译 :生成)
SpannableString ss = new SpannableString( "绿色打电话粗体删除线红色下划线图片:.斜体背景阴影");
//字体颜色
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 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(Color.RED), 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, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //放入图片
//斜体
ss.setSpan(new StyleSpan(Typeface.ITALIC), 19, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//背景阴影
ss.setSpan(new BackgroundColorSpan(Color.YELLOW), 21, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//text 和 EditText 同样适用
text4.setText(ss);
text4.setMovementMethod( LinkMovementMethod.getInstance());
edittext5.setText(ss);
edittext5.setMovementMethod( LinkMovementMethod.getInstance());
edittext5.setBackgroundResource(android.R.drawable.editbox_background_normal);
/** Text6 的阴影效果
*
* android:textSize="28sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:shadowColor="#ff000000" //阴影颜色
android:shadowDx="2" //阴影x偏移量
android:shadowDy="2" //Y偏移量
android:shadowRadius="1"/> //阴影的范围
--------------------------------------------------------
或者设置 style
<resources>
<style name="StyleBarTitle">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:textSize">@dimen/text_size_vlarge</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:shadowColor">#ff000000</item>
<item name="android:shadowDx">2</item>
<item name="android:shadowDy">2</item>
<item name="android:shadowRadius">1</item>
<item name="android:background">@null</item>
</style>
</resources>
*/
我们知道要让TextView解析和显示Html代码。可以使用 Spanned text = Html.fromHtml(source);
tv.setText(text);
来实现,这个用起来简单方便。
但是,怎样让TextView也显示Html中节点的图像呢?
我们可以看到fromHtml还有另一个重构:
fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
实现一下ImageGetter就可以让图片显示了:
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
drawable = Drawable.createFromPath(source); // Or fetch it from the URL
// Important
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
.getIntrinsicHeight());
return drawable;
}
};
至于TagHandler,我们这里不需要使用,可以直接传null。