Resource Type

来源:互联网 发布:淘宝名不虚传 生意参谋 编辑:程序博客网 时间:2024/05/05 02:17

String Resources

String


    最简单的字符串,可以从程序或者其他资源文件(比如一个XML布局)引用。

Note:字符串是简单的资源,使用它的name属性引用。所以可以在一个XML文件混合String资源和其他简单资源(比如color),在一个<resources>元素下面。

使用例子:

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

You can use either getString(int)or getText(int)to retrieve a string. getText(int)will retain any rich text styling applied to the string.

语法:

<resources>

Required. This must be the root node.

No attributes.

<string>

A string, whichcan include styling tags. Beware that you must escape apostrophes and quotation marks. For more information abouthow to properly style and format your strings seeFormattingand Styling, below.

attributes:

name

String. A name for the string. This name will be used as theresource ID.

 

StringArray


String数据,比如Adapter需要填充的内容,这样定义比较好吧,比死代码好。

Note:一个String数组是一个简单资源 ,使用name属性指定。也可以好而其他简单资源混合在<resources>元素下。

语法元素:

<resources>

Required. This must be the root node.

No attributes.

<string-array>

Defines an arrayof strings. Contains one or more <item> elements.

attributes:

name

String. A name for the array. This name will be used as theresource ID to reference the array.

<item>

A string, whichcan include styling tags. The value can be a reference to another stringresource. Must be a child of a <string-array> element. Beware that youmust escape apostrophes and quotation marks. SeeFormattingand Styling, below, for information about to properly style and format yourstrings.

No attributes.

使用例子:

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

 

 

QuantityStrings (Plurals)


量词,复数的表示,用到的时候在说吧。

Formattingand Styling


格式化字符串

Escaping apostrophes and quotes

如果在string中要用单引号(’),要么使用\转义,要么使用双引号括起来。下面是错的:

<string name="good_example">This\'ll work</string><string name="good_example_2">"This'll also work"</string><string name="bad_example">This doesn't work</string>    <!-- Causes a compile error -->

 

但是,如果要使用双引号,必须使用\转义看,单引号的不行的。

<string name="good_example">This is a \"good string\".</string><string name="bad_example">This is a "bad string".</string>    <!-- Quotes are stripped; displays as: This is a bad string. --><string name="bad_example_2">'This is another "bad string".'</string>    <!-- Causes a compile error -->

 

Formatting strings

如果需要使用String.format(String,Object…)格式化字符串,可以把格式化参数放在string资源中。例如,下面的资源:

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

Styling with HTML markup

You can add styling to your strings with HTML markup. For example:

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

Supported HTML elements include:

  • <b> for bold text.
  • <i> for italic text.
  • <u> for underline text.

有时候可能想创建一个格式化文本资源特能用format string。通常,这不会工作,因为String.format(String,Object...)剥离所有格式化信息从字符串。

Sometimes you may want to create a styled text resource that is also usedas a format string. Normally, this won't work because theString.format(String,Object...) method willstrip all the style information from the string. The work-around to this is towrite the HTML tags with escaped entities, which are then recovered withfromHtml(String),after the formatting takes place. For example:

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

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

In this formatted string, a <b> element is added.Notice that the opening bracket is HTML-escaped, using the &lt; notation.

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

Because the fromHtml(String)method will format all HTML entities, be sure to escape any possible HTMLcharacters in the strings you use with the formatted text, usinghtmlEncode(String).For instance, if you'll be passing a string argument to String.format()that may contain characters such as "<" or "&", thenthey must be escaped before formatting, so that when the formatted string ispassed throughfromHtml(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);

 

Bool

都是简单资源,都可以混合使用。

example:

XML file saved at res/values-small/bools.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="screen_small">true</bool>
    <bool name="adjust_view_bounds">true</bool>
</resources>

This application code retrieves the boolean:

Resources res = getResources();
boolean screenIsSmall = res.getBoolean(R.bool.screen_small);

This layout XML uses the boolean for an attribute:

<ImageView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:src="@drawable/logo"
    android:adjustViewBounds="@bool/adjust_view_bounds" />

 

 

Color


Color也是简单资源,格式如下,我们可以使用drawable设置为一个color。

android:drawable="@color/green"

The value always begins with a pound (#) character and thenfollowed by the Alpha-Red-Green-Blue information in one of the followingformats:

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

使用例子:

XML file saved at res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
</resources>

This application code retrieves the color resource:

Resources res = getResources();
int color = res.getColor(R.color.opaque_red);

This layout XML applies the color to an attribute:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

 

Dimension


一个dimension值定义在xml。尺寸,比如: 10px, 2in, 5sp.下面是单位:

dp

     密度独立像素,在不同的屏幕会根据屏幕尺寸缩放,我们应该经常用这个dp吧。

Density-independentPixels - An abstract unit that is based on the physical density of the screen.These units are relative to a 160 dpi (dots per inch) screen, on which 1dp isroughly equal to 1px. When running on a higher density screen, the number of pixelsused to draw 1dp is scaled up by a factor appropriate for the screen's dpi.Likewise, when on a lower density screen, the number of pixels used for 1dp isscaled down. The ratio of dp-to-pixel will change with the screen density, butnot necessarily in direct proportion. Using dp units (instead of px units) is asimple solution to making the view dimensions in your layout resize properlyfor different screen densities. In other words, it provides consistency for thereal-world sizes of your UI elements across different devices.

sp

Scale-independentPixels - This is like the dp unit, but it is also scaled by the user's fontsize preference. It is recommend you use this unit when specifying font sizes,so they will be adjusted for both the screen density and the user's preference.

推荐在设置font大小的时候使用,上面dp应该是layout上使用吧。

pt

Points - 1/72 ofan inch based on the physical size of the screen.

px

Pixels -Corresponds to actual pixels on the screen. This unit of measure is notrecommended because the actual representation can vary across devices; eachdevices may have a different number of pixels per inch and may have more orfewer total pixels available on the screen.

不推荐,相对于屏幕的真实像素,但是不同屏幕是不一的。

mm

Millimeters -Based on the physical size of the screen.

in

Inches - Basedon the physical size of the screen.

 

 

使用例子,估计最多就是dp和sp了。Dp给图,sp给字

XML file saved at res/values/dimens.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_height">25dp</dimen>
    <dimen name="textview_width">150dp</dimen>
    <dimen name="ball_radius">30dp</dimen>
    <dimen name="font_size">16sp</dimen>
</resources>

This application code retrieves a dimension:

Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);

This layout XML applies dimensions to attributes:

<TextView
    android:layout_height="@dimen/textview_height"
    android:layout_width="@dimen/textview_width"
    android:textSize="@dimen/font_size"/>

 

ID


定义在XML中的唯一资源ID。使用<item>元素中个名字,安卓系统在R类中创建一个唯一的整数。可以用来指定一个应用资源。一个view,或者一个唯一的整数,(一个ID作为对话框的结果code)

Note:简单资源,可以混合,但是还是不要吧。并且,记住,一个ID资源部代表一个真实的资源,仅仅是添加到其他资源的简单id或者作为一个唯一的整数在应用中,但是只要指定了一个资源,就可以用了呀。

XML file saved at res/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="button_ok" />
    <item type="id" name="dialog_exit" />
</resources>

Then, this layout snippet uses the "button_ok" ID for a Buttonwidget:

<Button android:id="@id/button_ok"
    style="@style/button_style" />

引用的时候不用在id前有+了,因为已经存在了。有+是不存在的。

 

Integer


An integer defined in XML.

整数,也是简单资源,可以混合。XML file saved atres/values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="max_speed">75</integer>
    <integer name="min_speed">5</integer>
</resources>

This application code retrieves an integer:

Resources res = getResources();
int maxSpeed = res.getInteger(R.integer.max_speed);

 

IntegerArray


和string数组类似吧。

例子:

XML file saved at res/values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="bits">
        <item>4</item>
        <item>8</item>
        <item>16</item>
        <item>32</item>
    </integer-array>
</resources>

This application code retrieves the integer array:

Resources res = getResources();
int[] bits = res.getIntArray(R.array.bits);

 

TypedArray


类型数组,可以包含其他资源,比如drawable.这个数组不要求是同种的,可以创建会和类型资源数组,但是自己知道哪些哪里有,这样可以在get方法适当获得每一个。

例子:

XML file saved at res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

This application code retrieves each array and then obtains the firstentry in each array:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

 

 

 

0 0