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

      

      

  • 相关阅读:
    三位数
    顺序表应用4:元素位置互换之逆置算法
    顺序表应用5:有序顺序表归并
    顺序表应用6:有序顺序表查询
    数据结构实验之图论八:欧拉回路
    串结构练习——字符串连接 Description
    图的基本存储的基本方式三
    数据结构实验之图论四:迷宫探索
    数据结构实验之图论二:图的深度遍历
    图的基本存储的基本方式二
  • 原文地址:https://www.cnblogs.com/iMirror/p/4071351.html
Copyright © 2020-2023  润新知