A resource in Android refers to a non-code part that refers to an external file.
Assets is generally stored in native files, such as MP3 files, the Android program is not directly accessible and must be read in the form of a binary stream 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 identities of all the resource files used in the system.
1. Using resource files in your code
Accessing a resource file in code is accessed through the resource file type and resource file name defined in the R class.
The specific format is:
R. resource file type. resource file name
In addition, you can access resource files in the system, in addition to accessing user-defined resource files.
The format of the resource file in the access system is :
Android. R. resource file type. resource file name
2. Referencing a resource file in another resource file
Images, color resources, string resources, and dimension resources are often referenced in layout files.
The general format for referencing resources in other resources is:
@[Package Name:] Resource type/resource name
Working with color resources
The color value defines the beginning of a # number, followed by the ALPHA-RGB format.
For example:
#RGB
#ARGB
#RRGGBB
#AARRGGBB
Reference resource format:
In Java code: R.color.color_name
In the XML file: @[package:]color/color_name
When used in the res\values\ directory, define a colors.xml file, which contains the color name and color value of the key value pairs.
Such as:
<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 string resources in Java code r.string.string_name
Can be obtained as follows:
String str = getString (r.string.string_name). toString ();
Referencing a string resource in an XML file: @[package:]string/string_name
Working with Dimension Resources
Dimension resources are defined in the Res\values\dimens.xml file.
Size units supported in Android:
Unit representation |
Unit name |
Description |
Dip |
Device independent pixels |
Different device display effects, dip and screen density |
Px |
Pixel |
True pixels on the screen show the same effect on different devices display |
Inch |
Feet |
Screen-based physical dimensions |
Mm |
Mm |
Screen-based physical dimensions |
Pt |
Point (LB) |
Feet of 1/72 |
Dp |
Density-independent pixels |
Abstract unit of relative screen physical density |
Sp |
Pixels independent of precision |
Similar to DP, scale-independent pixels, primarily handling font sizes |
Reference Dimension Resources:
In the 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 accessed through the Resources.getxml () method.
The idea of getting the original XML file is to get the XML raw file by Getresources (). GETXML () to get the Xmlresourceparser object that determines whether the document starts or ends, whether it's the beginning or the end of a tag, It also accesses the contents of the XML file by traversing the XML file through some method of obtaining properties.
Using Drawables Resources
drawable resources are pictures or color resources that are used primarily to draw screens and are obtained through the resources.getdrawable () method.
drawable resources are divided into three categories : Bitmap File (bitmap), color drawable (color), Nine-patch image (nine pictures).
The bitmap files supported in Android are PNG, JPG, and GIF.
To reference the format of a bitmap resource:
In Java code: R.drawable.file_name
In the XML file: @[package:]drawable/file_name
Using Layout resources
Layout resources are one of the most common resources in Android that define how components in a screen are laid out in an XML file, similar to HTML pages in the Web.
The layout file is in res\layout\, and the name is arbitrary. Android uses the Layoutinflater class to resolve components in an XML file into a visual view component.
In activity, call the Activity.setcontentview () method, set the layout file to the activity's interface, and use the Findviewbyid () method to get the components in the layout.
To reference a layout file:
In Java code: r.layout.my_layout
In the XML file: @[package:]layout/my_layout
Using Menu Resources
There are two ways to create any view component: one by declaring it in a layout file, and another by creating it in code.
Menus in Android are divided into options menus, context menus, and submenus, all of 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 project Res\menu\ directory. Referenced by means of r.menu.my_menu.
The structure of a typical menu resource file:<menu> the root element, nested within the root element <item> and <group> child elements,<item> elements can also be nested <menu> form submenus.
Access to resources in Android