Layoutinflater in Android!

Source: Internet
Author: User

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
  1. <? XML version = "1.0"
  2. Encoding = "UTF-8"?>
  3. <Linearlayout
  4. Xmlns: Android = "http://schemas.android.com/apk/res/android"
  5. Android: Orientation = "vertical"
  6. Android: layout_width = "fill_parent"
  7. Android: layout_height = "fill_parent"
  8. >
  9. <Textview
  10. Android: layout_width = "fill_parent"
  11. Android: layout_height = "wrap_content"
  12. Android: text = "@ string/hello"
  13. />
  14. <Button
  15. Android: Id = "@ + ID/button"
  16. Android: layout_width = "wrap_content"
  17. Android: layout_height = "wrap_content"
  18. Android: text = "showcustomdialog"
  19. />
  20. </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
  1. <? XML version = "1.0"
  2. Encoding = "UTF-8"?>
  3. <Linearlayout
  4. Xmlns: Android = "http://schemas.android.com/apk/res/android"
  5. Android: Orientation = "horizontal"
  6. Android: layout_width = "fill_parent"
  7. Android: layout_height = "fill_parent"
  8. Android: padding = "10dp"
  9. >
  10. <Imageview Android: Id = "@ + ID/image"
  11. Android: layout_width = "wrap_content"
  12. Android: layout_height = "fill_parent"
  13. Android: layout_marginright = "10dp"
  14. />
  15. <Textview Android: Id = "@ + ID/text"
  16. Android: layout_width = "wrap_content"
  17. Android: layout_height = "fill_parent"
  18. Android: textcolor = "# fff"
  19. />
  20. </Linearlayout>

 

4. Modify the main program layouinflaterdemo. Java code as follows:

 

[Java]
View plaincopy
  1. Package com. Android. tutor;
  2. Import Android. App. activity;
  3. Import Android. App. alertdialog;
  4. Import Android. content. context;
  5. Import Android. OS. Bundle;
  6. Import Android. View. layoutinflater;
  7. Import Android. View. view;
  8. Import Android. View. View. onclicklistener;
  9. Import Android. widget. Button;
  10. Import Android. widget. imageview;
  11. Import Android. widget. textview;
  12. Public class layoutinflaterdemo extends activity implements
  13. Onclicklistener {
  14. Private button;
  15. Public void oncreate (bundle savedinstancestate ){
  16. Super. oncreate (savedinstancestate );
  17. Setcontentview (R. layout. Main );
  18. Button = (button) findviewbyid (R. Id. Button );
  19. Button. setonclicklistener (this );
  20. }
  21. @ Override
  22. Public void onclick (view v ){
  23. Showcustomdialog ();
  24. }
  25. Public void showcustomdialog ()
  26. {
  27. Alertdialog. Builder;
  28. Alertdialog;
  29. Context mcontext = layoutinflaterdemo. This;
  30. // Either of the following methods can be used
  31. /// Layoutinflater Inflater = getlayoutinflater ();
  32. Layoutinflater Inflater = (layoutinflater)
  33. Mcontext. getsystemservice (layout_inflater_service );
  34. View layout = Inflater. Inflate (R. layout. custom_diater, null );
  35. Textview text = (textview) layout. findviewbyid (R. Id. Text );
  36. Text. settext ("Hello, welcome to Mr Wei's blog! ");
  37. Imageview image = (imageview) layout. findviewbyid (R. Id. Image );
  38. Image. setimageresource (R. drawable. Icon );
  39. Builder = new alertdialog. Builder (mcontext );
  40. Builder. setview (layout );
  41. Alertdialog = builder. Create ();
  42. Alertdialog. Show ();
  43. }
  44. }

 

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.

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.