zoukankan      html  css  js  c++  java
  • Android字符串资源及其格式化

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

    1、纯文本字符串

          一般来说,使用纯文本字符串仅仅需要res/values目录下的一个XML文件(通常命名为res/values/strings.xml,可以使用其它的文件名替换strings),根元素为resources,希望编码为资源的每个字符串都有一个string子元素。String元素包含name特性,它标示了此字符串的唯一名称,还有一个文本元素,包含字符串的文本。

    字符串的表示分以下三种情况:

    a) 普通字符串(不含双引号(”)及单引号(’))。其在XML文件中如代码一所示定义。

    代码一:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello World!</string>  
    4. </resources>  

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

    代码二:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3. <string name="hello">"Hello' World!"</string>  
    4. </resources>  

    代码三:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello/' World!</string>  
    4. </resources>  

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

    代码四:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello/' World/"!</string>  
    4. </resources>  

    2、格式字符串

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

    代码五:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello World, my name is %1$s!</string>  
    4.     <string name="app_name">MyString</string>  
    5. </resources>  

    代码六:

    [cpp] view plaincopy
     
    1. TextView mytext = (TextView)findViewById(R.id.mystring);  
    2. String myname = getString(R.string.hello);  
    3. myname = String.format(myname, "clark");  
    4. mytext.setText(myname);  

    图1:

    3、样式字符串

          Android中可以使用<b>、<i>及<u>的轻量级HTML标记对字符串进行样式处理。有如下几种方法进行字符串的样式化。

    a) 直接将HTML标记写入字符串资源中,同时在布局文件中直接引用。这里给出一个例子,代码七为文件strings.xml中的内容,代码八为布局文件的内容,代码九则给出了Activity中onCreate函数中的内容,图2则给出了最终的效果图。这种情况下,能使用的HTML标记仅为<i>、<b>及<u>三种。

    代码七:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>  
    4.     <string name="app_name">MyString</string>  
    5. </resources>  

    代码八:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <TextView    
    8.     android:id="@+id/mystring"  
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="wrap_content"   
    11.     android:text="@string/hello"  
    12.     />  
    13. </LinearLayout>  

    代码九:

    [cpp] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4. }  

    图2:

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

    代码十:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>clark</u>!</string>  
    4.     <string name="app_name">MyString</string>  
    5. </resources>  

    代码十一:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <TextView    
    8.     android:id="@+id/mystring"  
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="wrap_content"   
    11.     android:text=""  
    12.     />  
    13. </LinearLayout>  

    代码十二:

    [cpp] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
    5.           
    6.     String myname = getString(R.string.hello);  
    7.     Spanned textspan = Html.fromHtml(myname);  
    8.     mytext.setText(textspan);  
    9. }  

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

    代码十三:

    [cpp] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
    5.     mytext.setText(getResources().getText(R.string.hello));  
    6. }  

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

    代码十四:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello World, my name is clark!</string>  
    4.     <string name="app_name">MyString</string>  
    5. </resources>  

    代码十五:

    [cpp] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <EditText    
    8.     android:id="@+id/mystring"  
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="wrap_content"   
    11.     android:text=""  
    12.     />  
    13. </LinearLayout>  

    代码十六:

    [cpp] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4.     EditText mytext = (EditText)findViewById(R.id.mystring);  
    5.     mytext.setText(R.string.hello);  
    6.     Spannable spn = mytext.getText();  
    7.     spn.setSpan(new BackgroundColorSpan(Color.GRAY), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
    8.     mytext.setText(spn);  
    9. }  

    图3:

    4、样式字符串格式化

    对样式字符串进行格式化,有以下几个步骤:

    a) 对字符串资源中的HTML标记进行转义。例如,&lt;b&gt;Hello World,&lt;/b&gt; &lt;i&gt;my name is&lt;/i&gt; &lt;u&gt;%1$s&lt;/u&gt;!

    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
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello"><b>Hello World,</b> <i>my name is</i> <u>%1$s</u>!</string>  
    4.     <string name="app_name">MyString</string>  
    5. </resources>  

    代码十八:

    [cpp] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState) {  
    2.     super.onCreate(savedInstanceState);  
    3.     setContentView(R.layout.main);  
    4.     TextView mytext = (TextView)findViewById(R.id.mystring);  
    5.     String myname = getString(R.string.hello);  
    6.     myname = String.format(myname, "clark");  
    7.     Spanned textspan = Html.fromHtml(myname);  
    8.     mytext.setText(textspan);  
    9. }  

    5、参考内容

    (1)、http://book.douban.com/subject/5353163/

    (2)、http://baike.baidu.com/view/5626871.html?fromTaglist

  • 相关阅读:
    UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)
    Codeforces 482E ELCA (LCT)
    Codeforces 798D Mike and distribution (构造)
    AtCoder AGC017C Snuke and Spells
    HDU 6089 Rikka with Terrorist (线段树)
    HDU 6136 Death Podracing (堆)
    AtCoder AGC032D Rotation Sort (DP)
    jenkins+python+kubectl实现批量更新k8s镜像
    Linux 下载最新kubectl版本的命令:
    jenkins X 和k8s CI/CD
  • 原文地址:https://www.cnblogs.com/android-blogs/p/4901536.html
Copyright © 2011-2022 走看看