Android advanced tutorial (5)-layoutinflater in Android!

Source: Internet
Author: User

Disclaimer: The copyright of the EOE article belongs to the author and is protected by law. The following information must be included in the hyperlink for reprinting.

Author: android_tutor

Address: http://my.eoe.cn/androidtutor/archive/1327.html

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:

 1 2 3 4 5 6 7 8 91011121314151617181920
<?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:

 1 2 3 4 5 6 7 8 91011121314151617181920
<?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:

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849
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 impleme CNT 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 MC Ontext = layoutinflaterdemo. this; // you can use either of the following methods: // layoutinflater Inflater = getlayoutinflater (); layoutinflater Inflater = (layoutinflater) mcontext. getsystemservice (layout_inflater_service); view layout = Inflater. inflate (R. layout. custom_dialog, 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.
So far today, I went to bed. Please leave a message if you have any questions ~ Thank you!

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.