Use of include tags in android

Source: Internet
Author: User

Use of include tags in android

Transferred from: http://blog.sina.com.cn/s/blog_a364999b01017gxi.html

The include tag in Android is a good solution to facilitate the overlay of controls.

But there are some areas to be aware of, the following is the project I have encountered a problem, to do this record, easy to see later.

Include label usage.

1. Create a new XML file, name Head.xml
The contents of the Head.xml file are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/index_linear_foot"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
>
<imageview
Android:id= "@+id/head_btn_refresh"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
</RelativeLayout>
2. Create a new layout file named Main.xml
The contents of the Main.xml file are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical"
>
<include android:layout_width= "fill_parent" android:layout_height= "wrap_content" layout= "@layout/head"/>
</LinearLayout>
Note: There is no ID specified for it in our include tag above.

3. Create a new mainactivity, set the layout file to Main.xml;

4. Suppose I now need to set the background picture in the code for the Relativelayout container in Head.xml.
The code is as follows:
Get Layout Container Object
Relativelayout head = (relativelayout) Findviewbyid (r.id.index_linear_foot);
Set a background picture
Head.setbackgroundresource (R.drawable.head);
This will be OK.

5. Just said that our include tag does not specify an ID for it, assuming that our current Main.xml file layout container is relativelayout, and I need to place a control under Head.xml. You need to use the unique properties of the Relativelayout layout container android:layout_below= "" property. You also need to specify the id attribute for the include.
then our Main.xml file becomes as follows:
<?xml version= "1.0" encoding= "Utf-8"?
<relativelayout xmlns:android= "http ://schemas.android.com/apk/res/android "
    android:layout_width=" Fill_parent "
    android:layout_height= "Fill_parent"
    >
<include Android:id= "@+id/main_head" android:layout_width= "fill_parent" android:layout_height= "wrap_content" layout= "@ Layout/head "/>
<listview
android:id=" @+id/listview "
android:layout_width=" Fill_parent "
android:layout_height= "Wrap_content"
android:layout_below= "@+id/main_headb"
/>
</relativelayout
Then we are running our instance, and the result is that the code is running to Head.setbackgroundresource (R.drawable.head);
This sentence is thrown out of the ordinary.
Java.lang.NullPointerException

Original: If the include specified the ID, you can not directly use it as a control in the main XML directly obtained, you must first obtain the XML layout file, and then through the layout file Findviewbyid to obtain its child controls.
The code is as follows
View layout = Getlayoutinflater (). Inflate (r.layout.head, NULL);
Relativelayout head= (relativelayout) Layout.findviewbyid (r.id.index_linear_foot);
Set a background picture
Head.setbackgroundresource (R.drawable.head);
That's all you can do.

Use of include tags in android

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.