Usually, many controls will be used in this page, and the controls will use a lot of resources. The Android system itself has many resources, including various strings, pictures, animations, styles and layouts, etc., which can be used directly in the application. This has many benefits. It can not only reduce memory usage, but also reduce part of the workload, and also reduce the size of the program installation package.
The following describes how to utilize system resources from several aspects.
1) Use the system-defined id
For example, we have an xml file that defines ListView. Generally, we will write code snippets similar to the following.
<ListView
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Here we define a ListView, and the id that defines it is "@+id/mylist". In fact, if there is no special requirement, you can use the system-defined id, similar to the following.
<ListView
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
To refer to the system id in the XML file, you only need to add the "@android:" prefix. If you are using system resources in Java code, it is basically the same as using your own resources. The difference is that classes need to be used to use the system's resources, rather than using R classes specified by the application. Here, if you want to get the ListView, you can use it to get it.
2) Utilize the system's picture resources
Suppose we define a menu in the application, and the xml file is as follows.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:andro>
<item
android:
android:title="attachment"
android:icon="@android:drawable/ic_menu_attachment" />
</menu>
The code snippet android:icon="@android:drawable/ic_menu_attachment" originally wanted to refer to the icon of "attachment" in the existing Menu in the system. However, after the Build project, an error occurred. The prompt information is as follows:
error: Error: Resource is not public. (at 'icon' with value '@android:drawable/ic_menu_attachment').
From the error message, we can probably see that since the resource is not disclosed, it cannot be directly referenced in our application. In this case, we can find the corresponding image resources in the Android SDK, copy them directly to our project directory, and then use a code snippet like android:icon="@drawable/ic_menu_attachment" for reference.
The benefits of doing this are that the artist does not need to repeat the existing picture, which can save a lot of working hours; the other is that it can ensure that the style of our application is consistent with the system.
Experience sharing:
There are no public resources in Android, and direct references in XML will result in an error. In addition to finding the corresponding resources and copying them to our own application directory for use, we can also change the reference "@android" to "@*android" to solve the problem. For example, the attachment icon quoted above can be modified into the following code.
android:icon="@*android:drawable/ic_menu_attachment"
After modification, build the project again and there will be no errors.
3) Utilize the system's string resources
Suppose we want to implement a Dialog, which has "OK" and "Cancel" buttons on it. You can use the following code to directly use the strings that come with Android system.
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@android:string/yes"/>
<Button
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@android:string/no"/>
</LinearLayout>
If you use the system string, multi-language environments are already supported by default. As mentioned above, @android:string/yes and @android:string/no are used directly. "OK" and "Cancel" will be displayed in simplified Chinese environment, and "OK" and "Cancel" will be displayed in English environment.
4) Utilize the system's Style
Suppose there is a TextView in the layout file to display the title of the window and use medium-sized fonts. The following code snippet can be used to define the Style of the TextView.
<TextView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
where android:textAppearance="?android:attr/textAppearanceMedium" is the style of the system. It should be noted that when using the system style, you need to prefix "?android:" in front of the resource you want to use, instead of "@android:".
5) Use the color definition of the system
In addition to the above-mentioned various system resources, the color defined by the system can also be used. The most commonly used in projects is the use of transparent colors. The code snippet is as follows.
android:background ="@android:color/transparent"
Experience sharing:
There are many resources in the Android system itself that can be used directly in the application. For details, you can enter the corresponding folder of Android-sdk to view. For example: you can enter $android-sdk$\platforms\android-8\data\res, and the system resources inside will be fully visible.
Developers need to spend some time familiarizing themselves with these resources, especially image resources and various Style resources, so that during the development process, they can think of relevant resources and use them directly.