Introduction to XML static resource usage for the Android control family _android

Source: Internet
Author: User
Tags xmlns
Learning objectives:
1. Learn how to set up and invoke XML resources in Android
2, master How to use XML and Java code for collaborative development interface
3, understand the role of R file

When you develop Android, you can always see a R.java file that is automatically generated by the system:

You must understand a few points:

1. R.java is generated automatically, 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 consistent with the names of the sub files under the Res file in the Android project (except that the drawable is automatically divided into high school and 3 grades):

Your project may not be the same as the file or code in the previous illustration, but they must conform to the rules described above

3. The static fields in these internal classes represent the IDs of a resource that must be unique and correspond to the resource one by one specified in res, as the 0x7f020000 in the previous figure must correspond to icon.png picture resources.

4, resources can be various types: such as pictures, XML, and so on, where each node in the XML can also have IDs, set IDs for them, the R file will automatically generate one by one corresponding IDs for them

5, after the ID is automatically written to R.java, you can access these IDs through code. But it doesn't make sense to simply get these IDs, such as 0x7f020000 doesn't represent anything, but the good news is that Android provides a way to get the resources for a given ID by code: through the Getresources () method of the View class, You can get an instance of the resources class, and then through the getxxxx (int id) method, you can get the specified type of resource based on the ID of the shape. Many other controls provide 2 versions of the assignment, dynamically setting values, and assigning values to the values in the resource file, as shown in the following illustration of the 2 overloaded versions of the TextView control SetText:

2 ways to lay out in an activity:

Pure Code layout:

Copy Code code as follows:

Initialize a TextView
TextView view = new TextView (this);
Set the text content displayed
View.settext ("This is a text display control");
to layout
View.setlayoutparams (New Layoutparams (LINEARLAYOUT.LAYOUTPARAMS.WRAP_CONTENT,LINEARLAYOUT.LAYOUTPARAMS.WRAP_ CONTENT));
Carrying this view with activity
This.setcontentview (view);


The layout of pure code, although flexible, but with a large amount of Java code, difficult to maintain and other shortcomings.

Layout of Xml+java:

XML under the Res\layout file is typically used to provide Android with special XML for layout scenarios.
To use it to layout, you need to show the introduction of XML namespaces: Xmlns:android=http://schemas.android.com/apk/res/android
And all the layout file attributes are "Android:" As the limit, the most commonly used is layount_width, layount_height, text and other attributes

If you want the text displayed by the control to come from a resource file, you can use the @ Qualifier + Resource folder +/+ resource ID, such as if you need to bind the Hello value in a string resource, you can use:

android:text= "@string/hello".
The following shows only a normal XML layout, and how to call the layout in the activity using code and host it.

Under the layout of the Main.xml:

Copy Code code 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 is a corresponding ID in the R.layout, which is the same as the XML file name of the layout file with no suffix name:
The This.setcontentview (R.layout.main) is arranged for the activity by the static ID corresponding to the Main.xml file;
In contrast to the "Pure Code layout" and "Xml+java" layouts, we can see that most of the time the latter has the advantage:
1. Complete separation of interface and business logic
2, after the program compiles, still can modify the layout file
3, the use of some Third-party tool layout, can even write almost no XML code
How the controls in the XML layout file generate IDs in R and get the control in code:
As mentioned earlier, when you import any resource file or create a new XML layout file, R.java generates the corresponding ID, and it also describes the use of the @ qualifier in the layout file for assigning a resource value to a control.
So in program code, you can get the layout file based on the ID of the layout file, so how do you get an instance of the specific control?
Unlike layout files, which automatically generate IDs, controls require you to manually configure them, as follows:
Android:id = "@+id/control name" so that when the layout file is saved, a class named ID is generated in R, which automatically produces an ID that is consistent with the name of the control.
Because all of the controls are derived from view, you can get the control instance of the specified ID through the This.findviewbyid (r.id. Control name) method in the activity, but note that you might need to cast to the type you want:
If the Hungarian nomenclature is used to create a button in XML, its id attribute: Android:id = "@+id/btnok",
In Activty, you can use the button btn = (button) This.findviewbyid (R.id.btnok), and the btn that you get is an instance of the button defined in the XML. Next, you can manipulate the button using the Btn.settext method.

Summary:

This paper comprehensively describes how to make full use of XML layout files to interface with Java in Android, so as to achieve the code separation between interface and logic. It also describes how to get the value of a static resource or an instance of a control, respectively, in XML and code.

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.