In the development of Android apps, the layout code of controls on the screen and the logic code for the program are usually separate. The layout code of the interface is placed in a separate XML file, which is a tree-type organization that controls the layout of the page. In general, many controls are used on this page, and the controls use a lot of resources. The Android system itself has a lot of resources, including a variety of strings, pictures, animations, styles and layouts, and so on, which can be used directly in the application. This can be a lot of benefits, both to reduce the use of memory, but also to reduce the amount of work, or to reduce the size of the program installation package.
Here are some ways to take advantage of system resources.
1) Use the system-defined ID
For example, we have an XML file that defines a ListView, in general, we will write code snippets similar to the following.
<listview Android:id= "@+id/mylist" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"/> |
Here we define a ListView, which defines its ID as "@+id/mylist". In fact, if there is no special requirement, you can take advantage of the system-defined ID, similar to the following.
<listview Android:id= "@android: Id/list" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"/> |
To reference the system ID in the XML file, simply add the "@android:" prefix. If you are using system resources in Java code, you are basically the same as using your own resources. The difference is that you need to use Android. The R class uses the system's resources rather than the R class specified by the application. Here if you want to get a ListView you can use Android.r.id.list to get it.
2) Utilize the picture resources of the system
Suppose we define a Menu,xml file in the application as follows.
<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item Android:id= "@+id/menu_attachment" Android:title= "Accessories" android:icon= "@android:d rawable/ic_menu_attachment"/> </menu> |
The code snippet android:icon= "@android:d rawable/ic_menu_attachment" is intended to refer to the icon for "attachments" in the menu already in the system. But after the build project, we found that there was an error. The following information is indicated:
Error:Error:Resource is isn't public. (at ' icon ' with value ' @android:d rawable/ic_menu_attachment '). |
It can be seen from the wrong message that it cannot be directly referenced in our application because the resource is not exposed. In this case, we can find the corresponding image resource in the Android SDK, copy it directly into our project catalog, and then use a code snippet like android:icon= "@drawable/ic_menu_attachment" to refer to it.
The advantage of this is that the artist does not need to repeat a copy of the existing picture, can save a lot of work hours, the other is to ensure that our application style and system consistent.
Experience Sharing: There are no publicly available resources in Android, and direct referencing in XML can be an error. In addition to finding the corresponding resource and copying it to our own application directory, we can also change the reference "@android" to "@*android". For example, the attachment icon referenced above can be modified to the following code. android:icon= "@*android:drawable/ic_menu_attachment" After the modification, build the project again, there will be no error. |
3) Utilize the system's string resources
Suppose we want to implement a dialog,dialog with "OK" and "Cancel" buttons. You can use the following code to directly use the string that comes with the Android system.
<linearlayout android:orientation= "Horizontal" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" > <button Android:id= "@+id/yes" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:layout_weight= "1.0" android:text= "@android: String/yes"/> <button Android:id= "@+id/no" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:layout_weight= "1.0" android:text= "@android: string/no"/> </LinearLayout> |
If you use a system string, the multilanguage environment is already supported by default. such as the above code, directly using @android:string/yes and @android:string/no, in the Simplified Chinese environment will show "OK" and "Cancel", in English environment will show "OK" and "Cancel".
4) Use the style of the system
Suppose there is a textview in the layout file that displays the title of the window, using a medium-sized font. You can use the following code snippet to define the style of the TextView.
<textview Android:id= "@+id/title" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:textappearance= "? Android:attr/textappearancemedium"/> |
Where android:textappearance= "? Android:attr/textappearancemedium" is the use of the system's style. It should be noted that using the system style, you need to add "Android:" as a prefix to the resource you want to use, instead of "@android:".
5) Use the system's color definition
In addition to the various system resources mentioned above, you can also use system-defined colors. The most commonly used in a project is the use of transparent colors. The code snippet is as follows.
Android:background = "@android: Color/transparent" |
Experience Sharing: The Android system itself has a lot of resources in the application can be directly used, specific, you can go to the corresponding folder in ANDROID-SDK to see. For example: can enter the $android-sdk$\platforms\android-8\data\res, inside of system resources is sweeping. Developers need to spend some time familiarizing themselves with these resources, especially the picture resources and various style resources, so that they can think of resources and use them directly during the development process. |
Optimization Series related blog posts:
Android Development Optimization--memory optimization for bitmap
Android Development optimization-using soft references and weak references
Optimized for Android development – from a code perspective
Android Development Optimization-UI optimization (1)
Android Development Optimization-UI optimization (2)
Android Development Optimization-UI optimization (3)
---------------------------------------------------------------------------
http://blog.csdn.net/arui319
The "Android Application development Solution" has been published, this article is part of the first draft. Welcome to buy Reading.
This article can be reproduced, but please keep the above author information.
Thank you.
---------------------------------------------------------------------------
From for notes (Wiz)
Android Memory optimizer 4-Interface UI optimization (1)