access to resources in Android
The resources in Android refer to the non code part, which refers to the external file.
Assets are typically stored in native files, such as MP3 files, and Android programs cannot be accessed directly, and must be read in binary streams through the Assetmanager class.
Resources in res can be accessed directly through the R resource class.
The R class is automatically generated, in which the corresponding inner class is generated based on different resource types, which contains the identity of all resource files used in the system.
1. Using resource files in your code
Access to resource files in code is accessed through the resource file types and resource file names defined in the R class.
The specific format is:
R. resource file type. resource file name
In addition, you can access resource files on your system in addition to accessing user-defined resource files.
The format for accessing resource files in the system is:
Android. R. resource file type. resource file name
2. Referencing resource files in other resource files
Pictures, color resources, string resources, and dimension resources are often referenced in the layout file.
The general format for referencing resources in other resources is:
@[Package Name:] Resource type/resource name
Using color Resources
The color value definition begins with a # number followed by the ALPHA-RGB format.
For example:
#RGB
#ARGB
#RRGGBB
#AARRGGBB
Referencing resource formats:
Java code: R.color.color_name
XML file: @[package:]color/color_name
When used in the Res\values\ directory, define a Colors.xml file that holds the key value pairs for color names and color values.
Such as:
Copy Code code as follows:
<resources>
<color name= "RED_BG" > #f00 </color>
<color name= "Blue_text" > #0000ff </color>
</resources>
Other resources, such as strings and dimensions, are similar methods.
Using string Resources
Creates a string resource file Strings.xml. The contents are also key-value pairs
Referencing a string resource in Java code r.string.string_name
Can be obtained as follows:
String str = getString (r.string.string_name). toString ();
To reference a string resource in an XML file: @[package:]string/string_name
using Dimension Resources
The dimension resource is defined in the Res\values\dimens.xml file.
Size units supported in Android:
Unit indicates |
Unit name |
Description |
Dip |
Device independent pixel |
Different equipment display effects, dip and screen density related |
Px |
Pixel |
Real pixels on the screen indicate that different devices display the same effect |
In |
Feet |
Physical dimensions based on the screen |
Mm |
Mm |
Physical dimensions based on the screen |
Pt |
Point (LB) |
Feet of 1/72 |
Dp |
and density-independent pixels |
Abstract units relative to the physical density of the screen |
Sp |
and precision-independent pixels |
Similar to a DP, a scale-independent pixel that mainly handles font size |
Referencing Dimension Resources:
In Java code: R.dimen.dimen_name
In the XML file: @[package:]dimen/dimen_name
using raw XML Resources
The XML file is defined in the Res\xml\ directory of the project and is accessed through the Resources.getxml () method.
The idea of getting the original XML file is to get the original XML file by Getresources (). GETXML (), and get the Xmlresourceparser object that determines whether the document starts or ends with the beginning or end of a label, The XML file is accessed by means of some methods of obtaining properties.
using Drawables Resources
The drawable resource is a picture or color resource that is used primarily to draw the screen and is obtained through the resources.getdrawable () method.
drawable resources are divided into three categories: Bitmap File (bit graphic), color drawable (color), Nine-patch image (nine pieces of picture).
The supported bitmap files in Android are PNG, JPG, and GIF.
Format for referencing bitmap resources:
Java code: R.drawable.file_name
XML file: @[package:]drawable/file_name
using layout (layout) Resources
Layout resources are one of the most common types of Android resources, defining the layout of components in a screen in an XML file, similar to HTML pages in the Web.
The layout file is located in res\layout\ with any name. android resolves a component in an XML file to a visual view component through the Layoutinflater class.
In the activity, call the Activity.setcontentview () method, set the layout file as an activity interface, and use the Findviewbyid () method to get the components in the layout.
Referencing a layout file:
Java code: R.layout.my_layout
XML file: @[package:]layout/my_layout
using Menu Resources
There are two ways to create any view component: one by declaring it in the layout file, and the other by creating it in code.
The menus in Android are divided into option menus, context menus, and submenus, which can be declared in an XML file and used in code through the Menuinflater class.
The menu resource file is also an XML file, placed under the engineering res\menu\ directory. Refer to it by means of a r.menu.my_menu.
The structure of a typical menu resource file is:<menu> the root element, nested in the root element <item> and <group> child element,<item> elements can also be nested <menu> to form a submenu.
Time and energy is limited, can not be a detailed example of each type of study, had to be in the actual application of the integrated and then slowly realized. Come on!