Analysis of Android development optimization: a detailed explanation of Interface UI optimization (1)

Source: Internet
Author: User

Usually, many controls are used on this page, and controls use a lot of resources. The Android system has many resources, including various strings, images, animations, styles, and la S, which can be directly used in applications. This method has many advantages, which can reduce the memory usage, reduce part of the workload, and reduce the size of the program installation package.

The following describes how to use system resources from several aspects.

1) use the system-defined id

For example, we have an xml file that defines ListView. In general, we will write code snippets similar to the following.

Copy codeThe Code is as follows: <ListView

Android: id = "@ + id/mylist"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"/>

Here we define a ListView and define its id as "@ + id/mylist ". In fact, if there are no special requirements, you can use the system-defined id, as shown below.Copy codeThe Code is as follows: <ListView

Android: id = "@ android: id/list"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"/>

To reference the System id in an xml file, you only need to add the "@ android:" prefix. If you use system resources in Java code, it is basically the same as using your own resources. The difference is that you need to use the android. R class to use system resources, rather than using the R class specified by the application. You can use android. R. id. list to obtain the ListView.

2) Use System image resources

Suppose we define a menu in the application. The xml file is as follows.

Copy codeThe Code is 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 = "attachment"

Android: icon = "@ android: drawable/ic_menu_attachment"/>

</Menu>

The code snippet android: icon = "@ android: drawable/ic_menu_attachment" is intended to reference the "attachment" icon in the existing Menu in the system. However, an error was found after the Build project. The message is as follows:Copy codeThe Code is as follows: error: Error: Resource is not public. (at 'icon 'with value' @ android: drawable/ic_menu_attachment ').

From the error message, we can see that because the resource is not made public, 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 android: icon = "@ drawable/ic_menu_attachment.

One advantage of doing so is that the artist does not need to duplicate an existing image, which can save a lot of work hours; the other is to ensure that the style of our application is consistent with that of the system.

Copy codeThe Code is as follows: Experience Sharing:

There are no public resources in Android, and an error will be reported if you reference them directly in xml. In addition to finding the corresponding resources and copying them to our own app 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 and no error will be reported.

3) use the system's string Resources

Suppose we want to implement a Dialog, which has the "OK" and "cancel" buttons. You can use the following code to directly use the string that comes with Android.

Copy codeThe Code is as follows: <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 the system string is used, the multi-language environment is supported by default. For example, the above Code directly uses @ android: string/yes and @ android: string/no. In the Simplified Chinese environment, "OK" and "cancel" are displayed ", "OK" and "Cancel" are displayed in the English environment ".

4) use the system's Style

Assume that the layout file contains a TextView that is used to display the title of the window and uses a medium font. You can use the following code snippet to define the Style of TextView.

Copy codeThe Code is as follows: <TextView

Android: id = "@ + id/title"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: textAppearance = "? Android: attr/textAppearanceMedium "/>

Android: textAppearance = "? Android: attr/textAppearanceMedium "is the system style. Note that to use the system style, you must add "? Android: "Is the prefix instead of" @ android :".

5) use the color DEFINITION OF THE SYSTEM

In addition to the above various system resources, you can also use the color defined by the system. The most commonly used in projects is the use of transparent colors. The code snippet is as follows.

Copy codeThe Code is as follows: android: background = "@ android: color/transparent"

Experience Sharing:

The Android system has many resources that can be directly used in applications. For details, go to the corresponding folder of android-sdk to view them. For example, you can go to $ android-sdk $ \ platforms \ android-8 \ data \ res, and the system resources in it will be at a glance.

Developers need to spend some time to familiarize themselves with these resources, especially image resources and various Style resources, so that they can think of relevant resources and use them directly during the development process.

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.