Android string resource and its formatting

Source: Internet
Author: User
<span id="Label3"></p><p><p>In the Android Project layout, the resource is stored in the res/directory as an XML file. For better internationalization and localization, the string set is usually stored as an XML file in the Res/values/directory.</p></p><p><p></p></p>1. Plain Text string<p><p>In general, using plain text strings requires only an XML file in the Res/values directory (typically named res/values/ strings.xml, you can replace strings with other filenames), the root element is resources, and each string that you want to encode as a resource has a string child Element. The string element contains the name attribute, which indicates a unique name for this string, and a text element that contains the text of the String.</p></p><p><p>The representation of a string is divided into the following three scenarios:</p></p><p><p></p></p><p><p>A) Ordinary string (without double quotation marks (") and single quotation marks (')). It is defined in the XML file as shown in code ONE.</p></p><p><p>Code one:</p></p><p><p></p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" >hello world!</string></li> <li></resources></li> </ol> </ol><p><p></p></p><p><p></p></p><p><p>B) The string contains only single Quotes. It is defined in an XML file as shown in code two or code three (using the escape character backslash "/").</p></p><p><p>Code two:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" > "hello ' world!" </string></li> <li></resources></li> </ol> </ol><p><p></p></p><p><p>Code three:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" >hello/' world!</string></li> <li></resources></li> </ol> </ol><p><p></p></p><p><p>C) Strings in other Cases. It is defined in the XML file as shown in code three, even if it is escaped with a forward Backslash. Code four gives an example.</p></p><p><p>Code Four:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" >hello/' world/"!</string></li> <li></resources></li> </ol> </ol><p><p></p></p><p><p></p></p>2. format string<p><p>Android supports format strings as well as other implementations of Java. Here the string contains placeholders that represent the data to be replaced at run time with variable information (for example, Hello everyone, My name is%1 $ s). The placeholder is outlined as Follows: it is tagged with the%[index]$[type] format, The index tag replaces the position of the index resource in the resource, and type indicates the kind of resource to be replaced (s means that the resource is in string format). Here is an example of a format string, code five for the contents of the file strings.xml, code six for the string substitution of Java code, Figure 1 gives the Final.</p></p><p><p>Code five:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" >hello world, My name is%1$s!</string></li> <li><string name= "app_name" >MyString</string></li> <li class="alt"></resources></li> </ol> </ol><p><p></p></p><p><p>Code six:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">TextView mytext = (TextView) Findviewbyid (r.id.mystring);</li> <li>String myname = getString (r.string.hello);</li> <li class="alt">myname = String.Format (myname, "clark");</li> <li>Mytext.settext (myname);</li> </ol> </ol><p><p></p></p><p><p>Figure 1:</p></p><p><p></p></p><p><p></p></p>3. Style string<p><p>You can style strings with lightweight HTML tags for <b>, <i>, and <u> in Android. There are several ways to style strings.</p></p><p><p>A) directly write the HTML tag to the string resource and reference it directly in the layout file. Here is an example, code seven for the contents of the file strings.xml, code eight for the contents of the layout file, code nine gives the activity in the OnCreate function of the content, Figure 2 gives the Final. In this case, the HTML tags can be used only for <i>, <b> and <u> three Types.</p></p><p><p>Code seven:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" ><b>hello world,</b> <i>my name is</i> <u>clark</u>! </string></li> <li><string name= "app_name" >MyString</string></li> <li class="alt"></resources></li> </ol> </ol><p><p></p></p><p><p>Code eight:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li></li> <li><li><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"</li></li> <li class="alt"><li class="alt">android:orientation= "vertical"</li></li> <li><li>Android:layout_width= "fill_parent"</li></li> <li class="alt"><li class="alt">android:layout_height= "fill_parent"</li></li> <li><li>></li></li> <li class="alt"><li class="alt"><textview</li></li> <li><li>Android:id= "@+id/mystring"</li></li> <li class="alt"><li class="alt">Android:layout_width= "fill_parent"</li></li> <li><li>android:layout_height= "wrap_content"</li></li> <li class="alt"><li class="alt">android:text= "@string/hello"</li></li> <li><li>/></li></li> <li class="alt"><li class="alt"></LinearLayout></li></li> </ol><p><p></p></p><p><p>Code nine:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">public void OnCreate (Bundle Savedinstancestate) {</li> <li>Super.oncreate (savedinstancestate);</li> <li class="alt">Setcontentview (r.layout.main);</li> <li>}</li> </ol> </ol><p><p></p></p><p><p>Figure 2:</p></p><p><p></p></p><p><p>B) use escaped HTML tags in the string resource file, and use Java code to convert string Resources. Here is an example, code ten is the contents of the file strings.xml, code 11 is the contents of the layout file, code 12 gives the activity in the OnCreate function of the content, its effect 2 is Shown.</p></p><p><p>Code ten:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" ><b>hello world,</b> <i>my name is</i> <u>clark</u>! </string></li> <li><string name= "app_name" >MyString</string></li> <li class="alt"></resources></li> </ol> </ol><p><p></p></p><p><p>Code Listing 11:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li></li> <li><li><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"</li></li> <li class="alt"><li class="alt">android:orientation= "vertical"</li></li> <li><li>Android:layout_width= "fill_parent"</li></li> <li class="alt"><li class="alt">android:layout_height= "fill_parent"</li></li> <li><li>></li></li> <li class="alt"><li class="alt"><textview</li></li> <li><li>Android:id= "@+id/mystring"</li></li> <li class="alt"><li class="alt">Android:layout_width= "fill_parent"</li></li> <li><li>android:layout_height= "wrap_content"</li></li> <li class="alt"><li class="alt">android:text= ""</li></li> <li><li>/></li></li> <li class="alt"><li class="alt"></LinearLayout></li></li> </ol><p><p></p></p><p><p>Code Listing 12:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">public void OnCreate (Bundle Savedinstancestate) {</li> <li>Super.oncreate (savedinstancestate);</li> <li class="alt">Setcontentview (r.layout.main);</li> <li>TextView mytext = (TextView) Findviewbyid (r.id.mystring);</li> <li class="alt"></li> <li>String myname = getString (r.string.hello);</li> <li class="alt">spanned textspan = html.fromhtml (myname);</li> <li>Mytext.settext (textspan);</li> <li class="alt">}</li> </ol> </ol><p><p></p></p><p><p>C) Write HTML tags directly into the string resource and use Java code to convert the string resources. Here is an example, code seven is the contents of the file strings.xml, code 11 is the contents of the layout file, code 13 gives the activity in the OnCreate function of the content, its effect 2.</p></p><p><p>Code Listing 13:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">public void OnCreate (Bundle Savedinstancestate) {</li> <li>Super.oncreate (savedinstancestate);</li> <li class="alt">Setcontentview (r.layout.main);</li> <li>TextView mytext = (TextView) Findviewbyid (r.id.mystring);</li> <li class="alt">Mytext.settext (getresources (). GetText (r.string.hello));</li> <li>}</li> </ol> </ol><p><p></p></p><p><p>D) use a plain text string in the string resource file, and use Java code to convert the string resource. Here is an example, the code 14 is the contents of the file strings.xml, the code is the contents of the layout file (note that the interface shows the edittext), code 16 gives the activity in the OnCreate function of the content, the effect 3 is Shown. This can also be defined directly in Java code when defining a String.</p></p><p><p>Code Listing 14:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" >hello world, My name is clark!</string></li> <li><string name= "app_name" >MyString</string></li> <li class="alt"></resources></li> </ol> </ol><p><p></p></p><p><p>Code Listing 15:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li></li> <li><li><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"</li></li> <li class="alt"><li class="alt">android:orientation= "vertical"</li></li> <li><li>Android:layout_width= "fill_parent"</li></li> <li class="alt"><li class="alt">android:layout_height= "fill_parent"</li></li> <li><li>></li></li> <li class="alt"><li class="alt"><edittext</li></li> <li><li>Android:id= "@+id/mystring"</li></li> <li class="alt"><li class="alt">Android:layout_width= "fill_parent"</li></li> <li><li>android:layout_height= "wrap_content"</li></li> <li class="alt"><li class="alt">android:text= ""</li></li> <li><li>/></li></li> <li class="alt"><li class="alt"></LinearLayout></li></li> </ol><p><p></p></p><p><p>Code Listing 16:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">public void OnCreate (Bundle Savedinstancestate) {</li> <li>Super.oncreate (savedinstancestate);</li> <li class="alt">Setcontentview (r.layout.main);</li> <li>EditText mytext = (EditText) Findviewbyid (r.id.mystring);</li> <li class="alt">Mytext.settext (r.string.hello);</li> <li>Spannable spn = Mytext.gettext ();</li> <li class="alt">Spn.setspan (new backgroundcolorspan (color.gray), 0, one, spannable.span_exclusive_exclusive);</li> <li>Mytext.settext (spn);</li> <li class="alt">}</li> </ol> </ol><p><p></p></p><p><p>Figure 3:</p></p><p><p></p></p><p><p></p></p>4. Style string Formatting<p><p>To format a style string, there are several steps:</p></p><p><p>A) Escape the HTML tags in the string resource. Such as,&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;!</p></p><p><p>B) Retrieve string resources as a general rule. For example, getString (r.string.hello).</p></p><p><p>C) generate formatting results to ensure that any string values you replace are escaped, preventing them from containing angle brackets or & symbols. For example, String.Format (getString (r.string.hello), textutils.htmlencode (name)).</p></p><p><p>D) convert the entity escaped HTML to a spanned object through html.fromhtml (). For example, spanned textspan = html.fromhtml (myname).</p></p><p><p>Here is an example, code 17 is the contents of the file strings.xml, code 11 is the contents of the layout file, code 18 gives the activity in the OnCreate function of the content, the effect of 2 is Shown.</p></p><p><p>Code Listing 17:</p></p><p><p></p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><?xml version= "1.0" encoding= "utf-8"?></li> <li><resources></li> <li class="alt"><string name= "hello" ><b>hello world,</b> <i>my name is</i> <u>%1$s</u>! </string></li> <li><string name= "app_name" >MyString</string></li> <li class="alt"></resources></li> </ol> </ol><p><p></p></p><p><p>Code Listing 18:</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plaincopy <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">public void OnCreate (Bundle Savedinstancestate) {</li> <li>Super.oncreate (savedinstancestate);</li> <li class="alt">Setcontentview (r.layout.main);</li> <li>TextView mytext = (TextView) Findviewbyid (r.id.mystring);</li> <li class="alt">String myname = getString (r.string.hello);</li> <li>myname = String.Format (myname, "clark");</li> <li class="alt">spanned textspan = html.fromhtml (myname);</li> <li>Mytext.settext (textspan);</li> <li class="alt">}</li> </ol> </ol><p><p></p></p><p><p></p></p>5. Reference Content<p><p>(1), http://book.douban.com/subject/5353163/</p></p><p><p>(2), http://baike.baidu.com/view/5626871.html?fromTaglist</p></p><p><p>Android string resource and its formatting</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.