3.App Resources-Resource Types/String Resources

1. String

//saved at res/values/strings.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello!</string>
</resources>
//This layout XML applies a string to a View:<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

  This application code retrieves a string:

String string = getString(R.string.hello);

2. String Array

//saved at res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

  This application code retrieves a string array:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

3. Formatting and Styling

  3.1 Escaping apostrophes and quotes

//good
<string name="good_example">"This‘ll work"</string>
<string name="good_example_2">This\‘ll also work</string>
//bad
<string name="bad_example">This doesn‘t work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>

  3.2 Formatting strings

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

  In this example, the format string has two arguments:

     %1$s is a string and %2$d is a decimal number.

    You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

  3.3 Styling with HTML markup

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

  <b> for Bold text

  <i>  for italic text

  <u> for underline text

  Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won‘t work

  <1>Store your styled text resource as an HTML-escaped string:

//In this formatted string, a <b> element is added. Notice that the opening bracket is HTML-escaped, using the &lt; notation.<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>

  <2>Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);

  <3>Particularly. if you‘ll be passing a string argument to String.format() that may contain characters such as "<" or "&", then they must

    be escaped before formatting, so that when the formatted string is passed through fromHtml(String), the characters come out the way

    they were originally written. For example:

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);

  

  

时间: 2024-07-30 23:06:48

3.App Resources-Resource Types/String Resources的相关文章

String Resources

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String XML resource that provides a single string. String Array

More Resource Types

This page defines more types of resources you can externalize, including: Bool XML resource that carries a boolean value. Color XML resource that carries a color value (a hexadecimal color). Dimension XML resource that carries a dimension value (with

Android - Resource 之 String 小结

简单的string: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="string_name" >text_string</string> </resources> String string = getString(R.string.hello); string数组: <?xml version=&

Android - 资源(resource)转换为String

资源(resource)转换为String 本文地址: http://blog.csdn.net/caroline_wendy Android建议资源的动态绑定, 即把string写入资源内, 然后执行动态绑定. 有时需要把资源(resource)ID转换为相应的字符串String. 使用: String mystring = getResources().getString(R.string.mystring); 即可.

3.App Resources-Resource Types/Drawable Resources

1. Drawable Resources There are several different types of drawables as follow 2. Bitmap A bitmap image. Android supports bitmap files in a three formats: .png (preferred), .jpg (acceptable), .gif (discouraged). Bitmap files may be automatically opti

.net烦恼小问题之XML读取异常

现有一xml: <?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:build="http://schemas.microsoft.com/developer/appx/2012/build" IgnorableNamespaces="

About Oracle Cluster Registry

Oracle Cluster Registry (OCR) is a file that contains information about the cluster node list and instance-to-node mapping information. OCR also contains information about Oracle Clusterware resource profiles for resources that you have customized. T

Android数组资源

1.在values下创建arrays.xml文件 2.在arrays.xml中使用<string-array>或者<int-array>标签定义数组 name---定义数组的名字 item子类---用于定义数组的元素值 3.引用数组资源 在java代码中使用: Resources resource = getResources(); String[] cityName =  resource.getStringArray(R.array.city_names); (如果是定义的整型

许可和授权的研究及其破解

一个非常不错的office 07的界面控件.可以浏览下: http://www.divelements.com/net/ 非注册版本有30天的使用限制.使用.net reflector,可以看到关键地方: namespace Divelements.Util.Registration { ... 代码 private bool xa1d7cab22b1cb36a()        {            string str2;            AssemblyName name = A