Android mobile app development using the include tag

Source: Internet
Author: User

Android mobile app development using the include tag

In android, The include tag is a good solution to facilitate control overwriting.
However, there are also some points to note. Below is a problem I encountered in the project. Make this record for later viewing.
Include label usage.
1. Create an xml file named head. xml
The content of the head. xml file is as follows:

Android: id = "@ + id/index_linear_foot"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
>
Android: id = "@ + id/head_btn_refresh"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>

2. Create a new layout file named main. xml
The content of the main. xml file is as follows:

Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
>


Note: The include label above does not specify an id for it.
3. Create a MainActivity and set the layout file to main. xml;
4. Assume that I need to set a background image for the RelativeLayout container in head. xml in the code.
The Code is as follows:
// Obtain the layout container object
RelativeLayout head = (RelativeLayout) findViewById (R. id. index_linear_foot );
// Set the background image
Head. setBackgroundResource (R. drawable. head );
In this case, OK.
5. we just mentioned that our include label does not specify an id for it. Suppose our current main. the xml file layout container is RelativeLayout, and I need to put a control in the head. xml. You need to use the special property android: layout_below = "" of the RelativeLayout layout container. You also need to specify the id attribute for include.
The main. xml file is changed to the following:

Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

Android: id = "@ + id/listview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/main_headb"
/>

Then we are running our instance, and we find that the code is running to head. setBackgroundResource (R. drawable. head );
An exception is thrown during this sentence.
Java. lang. NullPointerException
It turns out that if include specifies an id, the controls in it cannot be directly obtained as controls in the main xml. The xml layout file must be obtained first, then obtain its child control through the layout file findViewById.
The Code is as follows:
View layout = getLayoutInflater (). inflate (R. layout. head, null );
RelativeLayout head = (RelativeLayout) layout. findViewById (R. id. index_linear_foot );
// Set the background image
Head. setBackgroundResource (R. drawable. head );
In this way, you can.

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.