[Android] Android development optimization-UI optimization (1)

Source: Internet
Author: User

During Android Application Development, the layout code of the control on the screen is usually separated from the logic code of the program. The layout code of the interface is stored in an independent XML file, which is organized in a tree and controls the layout of the page. 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.

<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.

<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.

<? 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:

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.

 

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.

<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.

<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.

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.

 

 

Blog related to optimization series:

Android development optimization-memory optimization for bitmap

Android development optimization-using soft references and weak references

Android development optimization-code optimization

Android development optimization-UI optimization (1)

Android development optimization-UI optimization (2)

Android development optimization-UI optimization (3)

---------------------------------------------------------------------------

Http://blog.csdn.net/arui319

An excellent solution for Android Application Development has been published. This article is part of the first draft. Welcome to purchase and read.

This article can be reproduced, but please keep the above author information.

Thank you.

---------------------------------------------------------------------------

 

 

 

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.