[Android performance optimization series] layout article <include> reuse layout, android Performance Optimization
If you like my blog, please pay attention to my Weibo, please click here (http://weibo.com/kifile), thank you
Reprinted please indicate the source (http://blog.csdn.net/kifile), thank you again
Address: http://developer.android.com/training/improving-layouts/reusing-layouts.html
In the next period of time, I will translate some official Android documents about performance improvement every day for you.
Performance Optimization Layout:
[Android performance optimization series] layout to reduce your interface level
Digress:
Most of the time, we will use similar la S. In this case, we may wish to extract the same layout and use it as a layout file separately, in this way, we avoid repeatedly writing the same code in multiple files. When we need to modify the code, we only need to modify the code in one place.
The following is the body of this article:
################
Although Android provides us with a series of controls to facilitate interaction, you may need to reuse some large components with specific la S. To reuse the layout more effectively, you should use <include/> and <merge/> to make one layout appear in another layout, rather than rewrite it in every layout file.
In this case, the reusable layout is particularly useful, and it allows you to create a complex reusable layout. For example, a yes/no button and a custom progress bar with text. This also means that some elements in your application are generic. So you can create a custom View for them separately, so that you can reuse the layout more conveniently.
Create a reusable Layout
If you already know which la s you want to use repeatedly, create a new layout file for them. For example, there is a layout from the G-Kenya Code lab. It defines a title bar, which will be referenced by every activity.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=”match_parent” android:layout_height="wrap_content" android:background="@color/titlebar_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /></FrameLayout>
This View should be consistent with the display effect you want in each Activity.
Use the <include> label
When you want to add a reusable component to another layout, you can use the <include/> label. For example, there is a piece of code from the G-Kenya Code lab. He wants to include the title bar mentioned above.
Here is his layout File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=”match_parent” android:layout_height=”match_parent” android:background="@color/app_bg" android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> <TextView android:layout_width=”match_parent” android:layout_height="wrap_content" android:text="@string/hello" android:padding="10dp" /> ...</LinearLayout>
Similarly, you can override the parameters of the layout file in the <include/> label, for example, the property similar to android: layout _ *, to define the properties of the contained layout, such
<include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>
If you want to use the <include> label to override the layout attribute, you must override layout_height and layout_width to make other attributes take effect.
Use the <merge> label
<Merge/> labels can effectively help us lower your layout level. For example, your layout is a vertical linear layout, and the layout you want to reuse is also a similar vertical linear layout. Then, using another linear layout as the root element for reusing the layout will lead to a vertical linear layout containing another vertical linear layout. This nested linear layout does not make any sense and will reduce your ui performance.
To avoid the above situation, you can use the <merge> label as the root element in the layout you reuse.
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/></merge>
In this way, when you use the <include/> label for reuse, the <merge> label is ignored, and then the two buttons are placed in the <include/> position.