R.bool.bool_name
@[package:]bool/bool_name
句法:
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="bool_name" >[true | false]</bool> </resources>ELEMENTS:
<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="screen_small">true</bool> <bool name="adjust_view_bounds">true</bool> </resources>此应用程序代码检索布尔:
Resources res =该布局XML使用一个属性布尔getResources()
; boolean screenIsSmall = res.getBoolean
(R.bool.screen_small);
<ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:src="@drawable/logo" android:adjustViewBounds="@bool/adjust_view_bounds" />
R.color.color_name
@[package:]color/color_name
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="color_name" >hex_color</color> </resources>ELEMENTS:
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>此应用程序代码检索颜色资源:
Resources res =这种布局XML应用颜色的属性:getResources()
; int color = res.getColor
(R.color.opaque_red);
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/translucent_red" android:text="Hello"/>
R.dimen.dimension_name
@[package:]dimen/dimension_name
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="dimension_name" >dimension</dimen> </resources>ELEMENTS:
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>此应用程序代码检索维度:
Resources res =这种布局XML适用尺寸属性:getResources()
; float fontSize = res.getDimension
(R.dimen.font_size);
<TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/>ID
R.id.name
@[package:]id/name
<?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="id_name" /> </resources>ELEMENTS:
<?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 Button widget:
<Button android:id="@id/button_ok" style="@style/button_style" />请注意,在android:值id不包括在ID参考的加号,因为该ID已经存在,如ids.xml例如上述定义。 (当使用加号,格式android的指定ID到一个XML资源:ID=“@+ ID /名称” - 它意味着“名”ID不存在,应创建)
showDialog
(R.id.dialog_exit);
在相同的应用程序,创建一个对话的时候,“对话退出”的ID进行比较:
protected Dialog onCreateDialog(int)
(int id) {
Dialog dialog;
switch(id) {
case R.id.dialog_exit:
...
break;
default:
dialog = null;
}
return dialog;
}
R.integer.integer_name
@[package:]integer/integer_name
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="integer_name" >integer</integer> </resources>ELEMENTS:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="max_speed">75</integer> <integer name="min_speed">5</integer> </resources>此应用程序代码检索一个整数:
Resources res =整数数组getResources()
; int maxSpeed = res.getInteger
(R.integer.max_speed);
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="integer_array_name"> <item >integer</item> </integer-array> </resources>ELEMENTS:
<?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);
<?xml version="1.0" encoding="utf-8"?> <resources> <array name="integer_array_name"> <item>resource</item> </array> </resources>ELEMENTS:
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>此应用程序代码检索每个数组,然后获得每个数组中的第一项:
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);