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

Android字符串资源及其格式化

2015-01-20 11:00 471 查看
在Android项目布局中,资源以XML文件的形式存储在res/目录下。为了更好的实现国际化及本地化,字符串集通常以XML文件的形式存储在res/values/目录下。

1、纯文本字符串

一般来说,使用纯文本字符串仅仅需要res/values目录下的一个XML文件(通常命名为res/values/strings.xml,可以使用其它的文件名替换strings),根元素为resources,希望编码为资源的每个字符串都有一个string子元素。String元素包含name特性,它标示了此字符串的唯一名称,还有一个文本元素,包含字符串的文本。
字符串的表示分以下三种情况:
a) 普通字符串(不含双引号(”)及单引号(’))。其在XML文件中如代码一所示定义。
代码一:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World!</string>
</resources>

b) 字符串仅含单引号。其在XML文件中如代码二或代码三(使用转义字符反斜杠“/”)所示定义。
代码二:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">"Hello' World!"</string>
</resources>

代码三:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello/' World!</string>
</resources>

c) 其它情况下的字符串。其在XML文件中如代码三所示定义,即使用一个前置反斜杠进行转义。代码四给出了一个示例。
代码四:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello/' World/"!</string>
</resources>

2、格式字符串

Android与Java的其它实现一样支持格式字符串。这里字符串包含一些占位符,表示在运行时要使用可变信息替换的数据(例如,Hello everyone, my name is %1$s)。占位符的规定简述如下:其使用%[index]$[type]格式进行标记,index标记替换资源中第index个资源对应的位置,type则标示所要替换的资源的类型(s表示资源为字符串格式)。这里给出一个格式字符串的例子,代码五为文件strings.xml中的内容,代码六为进行字符串替换时的Java代码,图1则给出了最终的效果图。
代码五:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, my name is %1$s!</string>
<string name="app_name">MyString</string>
</resources>

代码六:

[cpp] view plaincopy

TextView mytext = (TextView)findViewById(R.id.mystring);
String myname = getString(R.string.hello);
myname = String.format(myname, "clark");
mytext.setText(myname);

图1:



3、样式字符串

Android中可以使用<b>、<i>及<u>的轻量级HTML标记对字符串进行样式处理。有如下几种方法进行字符串的样式化。
a) 直接将HTML标记写入字符串资源中,同时在布局文件中直接引用。这里给出一个例子,代码七为文件strings.xml中的内容,代码八为布局文件的内容,代码九则给出了Activity中onCreate函数中的内容,图2则给出了最终的效果图。这种情况下,能使用的HTML标记仅为<i>、<b>及<u>三种。
代码七:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>
<string name="app_name">MyString</string>
</resources>

代码八:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mystring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

代码九:

[cpp] view plaincopy

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

图2:



b) 字符串资源文件中使用转义HTML标记,同时使用Java代码对字符串资源进行转化。这里给出一个例子,代码十为文件strings.xml中的内容,代码十一为布局文件的内容,代码十二则给出了Activity中onCreate函数中的内容,其效果如图2所示。
代码十:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>
<string name="app_name">MyString</string>
</resources>

代码十一:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mystring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>

代码十二:

[cpp] view plaincopy

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView mytext = (TextView)findViewById(R.id.mystring);

String myname = getString(R.string.hello);
Spanned textspan = Html.fromHtml(myname);
mytext.setText(textspan);
}

c) 直接将HTML标记写入字符串资源中,同时使用Java代码对字符串资源进行转化。这里给出一个例子,代码七为文件strings.xml中的内容,代码十一为布局文件的内容,代码十三则给出了Activity中onCreate函数中的内容,其效果如图2所示。
代码十三:

[cpp] view plaincopy

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView mytext = (TextView)findViewById(R.id.mystring);
mytext.setText(getResources().getText(R.string.hello));
}

d) 字符串资源文件中使用纯文本字符串,同时使用Java代码对字符串资源进行转化。这里给出一个例子,代码十四为文件strings.xml中的内容,代码十五为布局文件的内容(注意,界面显示的是EditText),代码十六则给出了Activity中onCreate函数中的内容,其效果如图3所示。这里在定义字符串的时候也可以将其直接定义在Java代码中。
代码十四:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, my name is clark!</string>
<string name="app_name">MyString</string>
</resources>

代码十五:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/mystring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>

代码十六:

[cpp] view plaincopy

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText mytext = (EditText)findViewById(R.id.mystring);
mytext.setText(R.string.hello);
Spannable spn = mytext.getText();
spn.setSpan(new BackgroundColorSpan(Color.GRAY), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(spn);
}

图3:



4、样式字符串格式化

对样式字符串进行格式化,有以下几个步骤:
a) 对字符串资源中的HTML标记进行转义。例如,<b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!
b) 按一般情况检索字符串资源。例如,getString(R.string.hello)。
c) 生成格式结果,确保转义你替换的任何字符串值,防止它们包含尖括号或&符号。例如,String.format(getString(R.string.hello),TextUtils.htmlEncode(name))。
d) 通过Html.fromHtml()将实体转义的HTML转换为Spanned对象。例如,Spanned textspan = Html.fromHtml(myname)。
这里给出一个例子,代码十七为文件strings.xml中的内容,代码十一为布局文件的内容,代码十八则给出了Activity中onCreate函数中的内容,其效果如图2所示。
代码十七:

[cpp] view plaincopy

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"><b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!</string>
<string name="app_name">MyString</string>
</resources>

代码十八:

[cpp] view plaincopy

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView mytext = (TextView)findViewById(R.id.mystring);
String myname = getString(R.string.hello);
myname = String.format(myname, "clark");
Spanned textspan = Html.fromHtml(myname);
mytext.setText(textspan);
}

5、参考内容

(1)、http://book.douban.com/subject/5353163/
(2)、http://baike.baidu.com/view/5626871.html?fromTaglist

转载自 http://blog.csdn.net/wsywl/article/details/6555959
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: