Use the include and merge labels in the android Layout

Source: Internet
Author: User

When we develop Android la s, many la s are often the same, at this time, we can use the <include/> and <merge/> labels to include complex la s in the required la S, reducing the compilation of repetitive code.

 

1. Create a reusable layout:

The following code describes the top bar titlebar. XML that appears for each acitivity in the application.

<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:id="@+id/title"               android:layout_width="wrap_content"               android:layout_height="wrap_content"                android:src="@drawable/gafricalogo" /></FrameLayout>

The above Root View is where framelayout will appear later.

2. Use the <include/> label:

In the layout of an activity in the application, the top bar is the above layout, so we can include the titlebar. xml above to achieve reuse. The layout code is as follows:

<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  android:id="@+id/new_title"              layout="@layout/titlebar"/>    <TextView android:layout_width=”match_parent”              android:layout_height="wrap_content"              android:text="@string/hello"              android:padding="10dp" />    ...</LinearLayout>

By obtaining the element ID, we can modify the attributes of the elements in the include tag. The following example shows how to modify the image in the titlebar in actiivty:

private View mTitleBar = null;private ImageView mTitleImageView = null;mTitleBar = findViewById(R.id.new_title);mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title);mTitleImageView.setImageResource(R.drawable.logo);

When using the include tag, We can overwrite the attributes of the Root View of the insert layout (all Android: Layout _ * attributes)

<include android:id=”@+id/news_title”         android:layout_width=”match_parent”         android:layout_height=”match_parent”         layout=”@layout/title”/>

To overwrite the attributes of the Root View of the insert layout, you must specify the attributes Android: layout_width and Android: layout_height to make other overwrite attributes take effect.

 

3. Use the <merge/> label

The merge label is used to remove the redundant view group generated when one layout is included to another layout. For example, there are two consecutive buttons in many layout s, so we make these two consecutive buttons into reusable layout ). When using the include tag, we must first organize the two buttons together with a view group such as linearlayout for other layout S, if liearlayout is the place where the include is used, two consecutive liearlayout is generated, which has no benefit in addition to reducing the UI performance. In this case, we can use the <merge/> label as the root of reusable layout.
View to avoid this problem.

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

When we use the <include/> label to reuse the above Code, the system ignores the merge element and places two consecutive buttons directly at the <include/> label.

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.