private void initView() {
// 字符串占位符
TextView txt01 = (TextView) findViewById(R.id.txt01)
txt01.setText(Html.fromHtml(String.format(getResources().getString(R.string.txt001), "张三", 20)))
// 复数字符串
TextView txt02 = (TextView) findViewById(R.id.txt02)
txt02.setText(getResources().getQuantityString(R.plurals.txt002, 2, 2))
//带有标签的文本无法使用[\n]只能使用[<p>][<br>] 其中[<p>]表示换两行,即中间隔一行;[<br>]表示换一行
TextView txt03 = (TextView) findViewById(R.id.txt03)
String str03 = "<font color='red'>Hello word 01 !</font><br>"
str03 += "<font color='#0000FF'><big><i>Hello word 02 !</i></big></font>"
str03 += "<font color='@" + android.R.color.black + "'><tt><b><u><big>Hello word 03 !</big></u></b></tt></font><p>"
str03 += "<big><a href='http://www.baidu.com'>百度:www.baidu.com</a></big>"
//将带有预定义标签的字符串转换成 charSequence 对象
CharSequence charSequence = Html.fromHtml(str03)
//设置显示文本
txt03.setText(charSequence)
//下面的语句非常重要,没有该语句。无法点击链接调用浏览器显示网页
txt03.setMovementMethod(LinkMovementMethod.getInstance())
TextView txt04 = (TextView) findViewById(R.id.txt04)
String str04 = "baidu URL:http://www.baidu.com\n"
str04 += "我的email:abcde@163.com\n"
str04 += "我的电话:12345678"
txt04.setText(str04)
txt04.setMovementMethod(LinkMovementMethod.getInstance())
getBaseContext()
TextView txt05 = (TextView) findViewById(R.id.txt05)
String str05 = "图像 1 <img src='phone'/><br>"
str05 += "图像 2 <img src='location'/><br>"
str05 += "图像 3 <img src='person'/><br>"
str05 += "图像 4 <a href='http://www.baidu.com'><img src='phone'></a>"
CharSequence charSequence05 = Html.fromHtml(str05, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(getResId(source))
//把图像按照50%等比压缩显示
if(source.equals("location")){
drawable.setBounds(0,0,drawable.getIntrinsicWidth()/2,drawable.getIntrinsicHeight()/2)
}else{
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight())
}
return drawable
}
},null)
txt05.setText(charSequence05)
txt05.setMovementMethod(LinkMovementMethod.getInstance())
}
private int getResId(String name) {
try {
//根据资源ID名获取Filed对象
Field field = R.drawable.class.getField(name)
return Integer.parseInt(field.get(null).toString())
} catch (NoSuchFieldException e) {
e.printStackTrace()
} catch (IllegalAccessException e) {
e.printStackTrace()
}
return 0
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
tools:context="cn.com.framework.fuxi001.MainActivity">
<TextView
android:id="@+id/txt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/txt02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/txt03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:autoLink="all"/>
<TextView
android:id="@+id/txt04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:autoLink="all"/>
<TextView
android:id="@+id/txt05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:autoLink="all"/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/layer"/>
</LinearLayout>
<resources>
<plurals name="txt002" >
<item quantity="one">一个人</item>
<item quantity="other">%d个人</item>
</plurals>
</resources>
<resources>
<string name="app_name">Fuxi001</string>
<string name="txt001"><b>姓名</b>:%1$s;<b>年龄</b>:%2$d;</string>
</resources>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="20dp"
android:left="20dp">
<bitmap android:src="@mipmap/ic_launcher"></bitmap>
</item>
<item android:top="30dp"
android:left="30dp">
<bitmap android:src="@mipmap/ic_launcher"></bitmap>
</item>
<item android:top="40dp"
android:left="40dp">
<bitmap android:src="@mipmap/ic_launcher"></bitmap>
</item>
</layer-list>