Use include in Android layout and points to be aware of

Source: Internet
Author: User

In android layout, use include to introduce another XML file as part of the layout, but when using include, you need to be aware of the following issues: first, use include to introduce a layout such as an existing title bar Block_ Header.xml, the code is as follows:<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/layout_header"Android:layout_width= "Match_parent"Android:layout_height= "@dimen/title_bar_h"Android:layout_alignparenttop= "true"Android:background= "@color/app_main_color"Android:paddingleft= "@dimen/bar_pd_left"Android:paddingright= "@dimen/bar_pd_left"android:gravity= "Bottom" > <ImageButton Android:id= "@+id/btn_back"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/back"Android:background= "@drawable/grid_item_selector"Android:layout_alignparentleft= "true"android:visibility= "Invisible"/> <textview android:id= "@+id/label_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "@dimen/title_size"Android:text= "title bar"Android:textcolor= "@color/white"Android:layout_centerhorizontal= "true"Android:layout_alignbottom= "@id/btn_back"Android:paddingbottom= "@dimen/bar_pd_bottom"/> <ImageButton Android:id= "@+id/btn_setting"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/setting"Android:background= "@drawable/grid_item_selector"Android:layout_alignparentright= "true"android:visibility= "Invisible"/> </RelativeLayout>now to introduce the layout of the title bar in Activity_main.xml, the code is as follows:<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity "> <include Android:id= "@+id/bolck_titlebar"Layout= "@layout/block_header"/> <TextView Android:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"Android:layout_below= "@id/bolck_titlebar"/> </RelativeLayout>second, in the Activity_main.xml loaded Activity.class, the operation of the Block_header.xml control "use of ordinary controls" is consistent with the operation of the control and the controls that are laid out directly in the Activity_main , such as setting the title of the title bar text as follows: TextView Tvtitle=(TextView) Findviewbyid (r.id.label_title); Tvtitle.settext ("title"); "Use of the outermost layout" but note that if you want to Block_ The outermost layout of the Header.xml is Layout_header, using Relativelayout layoutheader=(relativelayout) Findviewbyid (r.id.layout_header); obtained, the object obtained is NULL, because we set the id attribute for the Include section. If we do not set the id attribute, we can also manipulate it as above, for example, if we want to set the background color (there is no way to set ID for include): Relativelayout Layoutheader=(relativelayout) Findviewbyid (R.id.layout_header); Layoutheader.setbackgroundcolor (Color.BLUE); If we set the ID property, Some web pages are obtained and manipulated by the following methods (error practices): View layout= Getlayoutinflater (). Inflate (R.layout.block_header,NULL); Relativelayout Layoutheader=(relativelayout) Layout.findviewbyid (R.id.layout_header); Layoutheader.setbackgroundcolor (Color.BLUE); But through experiments, Does not achieve the effect we want, although set the background color, but in the activity_main.xml is still not set before the appearance, it is not difficult to explain, we get the object in this way is just Block_ Layout in Header.xml is not the layout we include in Activity_main.xml, when we set up the include Id,block_ in Activity_main.xml The outermost layout of the Header.xml has been mapped to the include, so just work with the include view, which is equivalent to block_header.xml the outermost layout, as follows (the practice of ID is set for include): view Layoutheader=Findviewbyid (R.id.bolck_titlebar); Layoutheader.setbackgroundcolor (color.blue); So when you operate on the outermost layout of the include layout, It is important to note that if the method is incorrect, there may be problems such as a null pointer error or an invalid setting. 

Use include in Android layout and points to be aware of

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.