Introduction to the use of XML static resources of Android control series

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 generated automatically, and it is strongly recommended that you do not modify the code manually. 2. The names of several internal classes in class R are consistent with the sub file names under the res file in the Android project (except that drawable is automatically divided into three levels of high, medium and low):

Your project may not be the files or code shown above, but they must comply with the above rules

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

4. Resources can be of various types, such as pictures, XML, etc. each node in XML can also have an ID. after setting the ID for them, the R file will automatically generate one-to-one corresponding IDs for them

5. After the IDs are automatically written to r.java, they can be accessed through code. However, simply obtaining these IDS is meaningless. For example, 0x7f020000 does not represent anything. Fortunately, Android provides a method to obtain the resources corresponding to the specified ID through code: you can obtain an instance of the resources class through the getresources() method of the view class, and then obtain the resources of the specified type according to the reshaped ID through the getxxxx (int ID) method. In addition, many controls provide two versions of assignment, which can dynamically set the value or assign it to the value in the resource file, as shown in the following figure, which shows two overloaded versions of textview control settext:

There are 2 ways to layout in activity:

Pure code layout:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>