From: hhttp: // blog.csdn.net/android_tutor/article/details/5513869
Hello everyone, this section describes the use of layoutinflater. In actual development, the layoutinflater class is very useful. Its function is similar to findviewbyid (),
The difference is that layoutinflater is used to find the XML layout file under layout and instantiate it! Findviewbyid () is used to find the widget controls (such as buttons and textview) in a specific XML file ).
In order to make it easy for everyone to understand, I made a simple demo with the main layout. XML contains a textview and a button. When a button is clicked, a dialog is displayed. The layout of this dialog is the custom_dialog.xml file defined in the layout directory (which is distributed left and right, imageview on the left, textview on the right ).
As follows:
The following describes the demo implementation process in detail:
1. Create an android project named layoutinflaterdemo.
2. Modify the main. xml layout. A button is added based on the original layout. The Code is as follows:
[Java]
View plaincopy
- <? XML version = "1.0"
- Encoding = "UTF-8"?>
- <Linearlayout
- Xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Textview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/hello"
- />
- <Button
- Android: Id = "@ + ID/button"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "showcustomdialog"
- />
- </Linearlayout>
3. Define the layout of the dialog box. In the layout directory, create a file named custom_dialog.xml. The Code is as follows:
[Java]
View plaincopy
- <? XML version = "1.0"
- Encoding = "UTF-8"?>
- <Linearlayout
- Xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: padding = "10dp"
- >
- <Imageview Android: Id = "@ + ID/image"
- Android: layout_width = "wrap_content"
- Android: layout_height = "fill_parent"
- Android: layout_marginright = "10dp"
- />
- <Textview Android: Id = "@ + ID/text"
- Android: layout_width = "wrap_content"
- Android: layout_height = "fill_parent"
- Android: textcolor = "# fff"
- />
- </Linearlayout>
4. Modify the main program layouinflaterdemo. Java code as follows:
[Java]
View plaincopy
- Package com. Android. tutor;
- Import Android. App. activity;
- Import Android. App. alertdialog;
- Import Android. content. context;
- Import Android. OS. Bundle;
- Import Android. View. layoutinflater;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Import Android. widget. imageview;
- Import Android. widget. textview;
- Public class layoutinflaterdemo extends activity implements
- Onclicklistener {
- Private button;
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Button = (button) findviewbyid (R. Id. Button );
- Button. setonclicklistener (this );
- }
- @ Override
- Public void onclick (view v ){
- Showcustomdialog ();
- }
- Public void showcustomdialog ()
- {
- Alertdialog. Builder;
- Alertdialog;
- Context mcontext = layoutinflaterdemo. This;
- // Either of the following methods can be used
- /// Layoutinflater Inflater = getlayoutinflater ();
- Layoutinflater Inflater = (layoutinflater)
- Mcontext. getsystemservice (layout_inflater_service );
- View layout = Inflater. Inflate (R. layout. custom_diater, null );
- Textview text = (textview) layout. findviewbyid (R. Id. Text );
- Text. settext ("Hello, welcome to Mr Wei's blog! ");
- Imageview image = (imageview) layout. findviewbyid (R. Id. Image );
- Image. setimageresource (R. drawable. Icon );
- Builder = new alertdialog. Builder (mcontext );
- Builder. setview (layout );
- Alertdialog = builder. Create ();
- Alertdialog. Show ();
- }
- }
5. click the button at the end of the execution to get the above results.
In short, layoutinflater is used to convert a. xml layout file to an instance of the View class.