Introduction to using XML static Resources in the Android control series

Source: Internet
Author: User
Tags crop image

Objective:
1. Learn how to set and call XML resources in Android
2. Master how to use XML and JAVA code for collaborative development interface
3. Understand the role of R files

When developing Android, you can always see an R. java file automatically generated by the system:

You must understand the following points:

1. R. java is automatically generated, and it is strongly recommended that you do not manually modify the code.
2. the names of several internal classes in the R class are the same as those of the sub-files in the res file of the Android project (except that drawable is automatically divided into three levels ):

The files or code in your project may not be as follows, but they must comply with the above rules.

Crop image resources.

4. resources can be of various types: slice, XML, etc. each node in XML can also have an ID, the R file will also automatically generate one-to-one corresponding IDs for them.

5. After IDs are automatically written to R. java, they can be accessed through code. However, obtaining these IDs is meaningless. For example, 0x7f020000 does not mean anything. Fortunately, Android provides a method to obtain resources corresponding to a specified ID through code: you can use the getResources () method of the View class to obtain an instance of the Resources class, and then use the GetXXXX (int id) method to obtain Resources of the specified type based on the integer ID. In addition, many controls provide two versions of value assignment, which can dynamically set values and assign values to values in resource files. For example, two overloaded versions of TextView control setText are displayed:

Two layout methods in Activity:

Code-only layout:

Copy codeThe Code is as follows: // initialize a TextView
TextView view = new TextView (this );
// Set the displayed text content
View. setText ("this is a text display control ");
// Layout
View. setLayoutParams (new LayoutParams (LinearLayout. LayoutParams. WRAP_CONTENT, LinearLayout. LayoutParams. WRAP_CONTENT ));
// Use Activity to carry this View
This. setContentView (view );

The Code-only layout is flexible, but it has many disadvantages such as large JAVA code and difficult to maintain.

XML + JAVA layout:

Xml in the res \ layout file is generally a special XML used to provide layout solutions for Android.
To use it for layout, You need to display the introduction XML namespace: xmlns: android = http://schemas.android.com/apk/res/android
All layout file attributes are limited by "android:". The most common attributes are layount_width, layount_height, and text.

If the text to be displayed by the control is from the resource file, you can use the @ qualifier + resource folder +/+ resource ID. to bind the hello value to the String resource, you can use:

Android: text = "@ string/hello ".
The following demonstrates only a common XML layout, and how to call this layout with code in the Activity, and bear it.

In Layout, main. xml:

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello wt! "
/>
</LinearLayout>

Then there will be a corresponding ID in R. layout. The name is the same as the xml file name of the layout file without the Suffix:
// Use the static ID corresponding to the main. xml file to layout this. setContentView (R. layout. main) for the Activity );
Compared with the "pure code layout" and "XML + JAVA" layout, we can find that the latter has more advantages in most cases:
1. completely separated interfaces and business logic
2. After the program is compiled, you can still modify the layout file.
3. Some third-party tools can be used for layout, and even XML code is almost unnecessary.
How to generate the ID in R by using the control in the XML layout file and obtain the control in the Code:
As mentioned above, after importing any resource file or creating a new XML layout file, R. java generates an ID corresponding to one of them. It also introduces that you can use the @ qualifier to assign resource values to controls in the layout file.
Therefore, in the program code, you can obtain the layout File Based on the layout File ID. How can you get an instance of a specific control?
Unlike the layout file that automatically generates an ID, the control requires you to manually configure it. The rules are as follows:
Android: id = "@ + id/Control name". After the layout file is saved, a class named id is generated in R, it also automatically generates an ID that is consistent with the control name.
Because all controls are derived from views, you can use this. findViewById (R. id. control name) method to obtain the control instance of the specified ID, but note that it may need to be forcibly converted to the type you need:
For example, if a button is created in XML using the Hungary name method, its ID attribute is android: id = "@ + id/btnOK ",
In Activty, you can use Button btn = (Button) this. findViewById (R. id. btnOK); The obtained btn is an instance of the Button defined in XML. Next, you can use btn. setText and other methods to operate the button.

Summary:

This article describes how to fully utilize XML layout files to build interfaces with Java in Android to achieve code separation between interfaces and logic. In addition, it also introduces how to obtain static resource values or control instances in XML and Code respectively.

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.