Resources in Android

Source: Internet
Author: User
Tags getcolor

In a nutshell, the resources in Android refer to non-code parts, slices, MP3, strings, XML files, and so on.
In an Android project, there are two folders alongside the SRC source folder, called Res and assets, which are used to save resource files.

Different points:
Resources in 1.res can be accessed directly through the R resource class. This method is more commonly used.
The res contains various subfolders that categorize the resources:
Anim (XML animation file), drawable (picture), layout (layout file), menu (menus), raw (binary), values (constant value), XML (XML file).
2.assets is generally stored in the original files, such as MP3 files, Android can not directly through the R class directly access, must be read through the Assertmanager class in the form of binary streams.

Second, the resources in Android are external files that are used in the code. These files are used as part of the application and are plainclothes into the application.
The general use of resources is divided into two ways:
1. Use the Getresources () method of the context in the code to get the resource object, which provides methods for obtaining various types of resources.
2. The general format for referencing resources in other resources is this:
1), @[package Name:] Resource type/resource name
2), R. Resource type. Resource Name

A) Use of color resources:
1), first create a new color.xml in the values directory:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<color name= "Text_color" > #cc0000 </color>
<color name= "Bg_color" > #99cc33 </color>
<color name= "Btntext_color" > #003399 </color>
<color name= "Btnbg_color" > #ffcc00 </color>
</resources>

2),
Take the button as an example:
public void Test (view view) {
Color
int i = This.getresources (). GetColor (R.color.bg_color);//Get color values
Toast.maketext (This, "" +i, 1). Show ();
This.getwindow (). Setbackgrounddrawableresource (R.color.bg_color);

}


B) Use of string resources:

Take the button as an example:
public void Test (view view) {

String
Button btn = (button) Findviewbyid (R.id.button1);
String str = this.getresources (). getString (R.string.welcome);
String str2 = this.getstring (r.string.welcome);//equivalent to the above
Btn.settext (STR2);


}

In an Android project, we may use a large number of strings as a cue message. These strings can be declared in the configuration file as string resources, thus implementing the program's configuration.
In code we use the Context.getstring () method to get the string by passing the resource ID parameter, or you can reference the string resource in another resource, in the format "@string/string resource name."
<resources>
<string name= "String_name" >STRING_VALUE</STRING>
</resources>
--------------------------------------------------------------------------
Resource Location Res/values/string.xml
--- -----------------------------------------------------------------------
Get String method context.getstring
----- ---------------------------------------------------------------------
Referencing resources in Java: R.string.string_name
In XML: @string/string_name
--------------------------------------------------------------------------

C) Use of size (dimens) Resources
We can use some common dimension units to define some text sizes, the width and height of the view components. A dimension resource is a numeric type of data that is defined in the Res/values/dimens.xml file. The
--------------------------------------------------------------------------
Unit represents the unit name Description
--------------- -----------------------------------------------------------
px pixel screen true pixels
---------------------------------- ----------------------------------------
in inch screen-based physical inches
-------------------------------------------------- ------------------------an abstract unit of
DP and density-independent pixels relative to the physical density of the screen
-------------------------------------------------------- ------------------
SP is similar to precision-independent pixels and DP
---------------------------------------------------------------------- ----


--------------------------------------------------------------------------
Resource Location Res/values/dimens.xml
--------------------------------------------------------------------------
XML format <resources> root element
<dimen name>value</dimen>
------------------------------------------------------------------ --------
Method GetResource () to obtain a size resource. getdimension ();
--------------------------------------------------------------------------
refers to the format of a dimension resource in Java code: R.dimen.dimen_name
XML file: @dimen/dimen_name
------------------------------------------------------------ --------------

Take the button as an example:
public void Test (view view) {
Size Resources
int color1 = Getresources (). GetColor (R.color.btntext_color);
Btn.settextcolor (Color1);//Set Button text color
int color2 = Getresources (). GetColor (R.color.btnbg_color);
Btn.setbackgroundcolor (COLOR2);//Set button background color

Float width = this.getresources (). Getdimension (R.dimen.button_width);
float height = this.getresources (). Getdimension (R.dimen.button_height);
Btn.setwidth ((int) width);
Btn.setwidth ((int) height);

}

D) Use of raw XML data
If some of the original XML files are used in the project, then we can define some XML files for the project to use. The XML file is defined in the Res/xml directory of the project.
--------------------------------------------------------------------------
Resource Location Res/xml
--------------------------------------------------------------------------
XML file format <resources> root node <someelement name=value/>
--------------------------------------------------------------------------
Gets the XML resource Method Getresources (). GETXML ()
--------------------------------------------------------------------------
The format of the reference XML resource in Java code, r.xml.xml_name
--------------------------------------------------------------------------
Take TextView, Button as an example:
1) Create a new XML directory under the Res directory and create the Users.xml under XML:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<user username= "Ckr" phone= "123456" ></user>
<user username= "WHF" phone= "456789" ></user>
</resources>
2)
public void Test (view view) {
XML resource
String text = "";
Xmlresourceparser XRP = This.getresources (). GETXML (R.xml.users);

while (Xrp.geteventtype ()! = xmlresourceparser.end_document) {
if (xrp.geteventtype () = = Xmlresourceparser.start_tag) {
String tagname = Xrp.getname ();
if (tagname.equals ("user")) {
String username = xrp.getattributevalue (0);
String phone = xrp.getattributevalue (1);
Text + = "User name:" + username + "; Phone: "+ phone +"; \ n ";
}

}
Xrp.next ();
}

TextView TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Tv.settext (text);

}


E) Use of drawable resources is a number of picture resources, mainly used to draw the screen, through the resources.getdrawable () method obtained.
drawable resources are divided into three categories: Bitmap File (bitmap), color drawable (color), Nine-patch image (nine pictures).
Commonly used are bitmaps: the bitmap files supported in Android are png,jpg and GIF.
--------------------------------------------------------------------------
Resource Location Res/drawable/filename.jpg
--------------------------------------------------------------------------
Ways to get bitmap resources in Java code: R.drawable.filename
XML file, @drawable/filename
--------------------------------------------------------------------------

To set a background picture with two buttons, first copy the picture (not the name of the image) to a directory that starts with drawable in the Res directory:
public void test1 (view view) {
drawable d = this.getresources (). getdrawable (R.DRAWABLE.A);
This.getwindow (). setbackgrounddrawable (d);
}

public void test2 (view view) {
drawable d = this.getresources (). getdrawable (r.drawable.b);
This.getwindow (). setbackgrounddrawable (d);
}
If set in global: Android:background= "@drawable/C

Resources in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.