Simple use of layoutinflater in Android

Source: Internet
Author: User

. Some background boxes are often used in the project, but the prospects are different. In this case, you can use layoutinflater.

CodeAs follows:

Main. xml

 
<? 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" Android: id = "@ + ID/main_layout"> <textview Android: layout_width = "fill_parent" Android: gravity = "center" Android: layout_height = "wrap_content" Android: TEXT = "@ string/INFO"/> <button Android: Id = "@ + ID/button1" Android: text = "@ string/dialog1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content"> </button> <button Android: Id = "@ + ID/button2" Android: TEXT = "@ string/dialog2" Android: layout_width = "match_parent" Android: layout_height = "wrap_content"> </button> </linearlayout>

Inflater. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout Android: Id = "@ + ID/relativelayout1" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" xmlns: Android = "http://schemas.android.com/apk/res/android"> <button Android: TEXT = "@ string/personal_cancel" Android: layout_width = "wrap_content" Android: Id = "@ + ID/button2" Android: layout_height = "wrap_content" Android: layout_alignbaseline = "@ + ID/button1" Android: layout_alignbottom = "@ + ID/button1" Android: layout_alignparentright = "true" Android: layout_marginright = "70dp"> </button> <edittext Android: layout_width = "100dp" Android: Id = "@ + ID/edittext1" Android: inputtype = "textpersonname" Android: layout_height = "wrap_content" Android: layout_alignparenttop = "true" Android: layout_centerhorizontal = "true"> <requestfocus> </edittext> <textview Android: layout_width = "wrap_content" Android: Id = "@ + ID/textview1" Android: text = "@ string/inputname" Android: layout_height = "wrap_content" Android: layout_alignbaseline = "@ + ID/edittext1" Android: layout_alignbottom = "@ + ID/edittext1" Android: layout_alignparentleft = "true" Android: textcolor = "# ffffff"> </textview> <button Android: text = "@ string/personal_save" Android: layout_width = "wrap_content" Android: id = "@ + ID/button1" Android: layout_height = "wrap_content" Android: layout_below = "@ + ID/edittext1" Android: layout_toleftof = "@ + ID/edittext1"> </button> </relativelayout>

Inflater2, XML

 
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout Android: Id = "@ + ID/rank_layout" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" xmlns: Android = "http://schemas.android.com/apk/res/android"> <textview Android: layout_width = "wrap_content" Android: Id = "@ + ID/textview1" Android: textappearance = "? Android: ATTR/textappearancelarge "Android: text =" @ string/second_inflater "Android: layout_height =" wrap_content "Android: Rule =" true "Android: layout_centerhorizontal =" true "Android: layout_margintop = "35dp"> </textview> </relativelayout>

Background. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "300dp" Android: layout_height = "150dp" Android: background = "@ drawable/BG" Android: Id = "@ + ID/bglayout"> </linearlayout>

Java code:

Inflateractivity, Java

Package Fover. inflater; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class inflateractivity extends activity implements onclicklistener {private button button1; private button button2;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); findview (); setonclick () ;}/ *** Description: Set the listener event */private void setonclick () {button1.setonclicklistener (this); button2.setonclicklistener (this );} /*** Description: Various views */private void findview () {button1 = (button) findviewbyid (R. id. button1); button2 = (button) findviewbyid (R. id. button2) ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. button1: intent = new intent (inflateractivity. this, personal. class); startactivity (intent); break; case R. id. button2: intent intent2 = new intent (inflateractivity. this, rank. class); startactivity (intent2); break; default: Break ;}}}

Personal. Java

 
Package Fover. inflater; import android. app. activity; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup. layoutparams; import android. view. window; import android. widget. linearlayout; public class personal extends activity {private view inflaterview; private linearlayout bgview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. background); bgview = (linearlayout) findviewbyid (R. id. bglayout); layoutinflater Inflater = layoutinflater. from (this); inflaterview = Inflater. inflate (R. layout. inflater, null); bgview. addview (inflaterview, new layoutparams (layoutparams. match_parent, layoutparams. match_parent ));}}

Rank. Java

Package Fover. inflater; import android. app. activity; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup. layoutparams; import android. view. window; import android. widget. linearlayout; public class rank extends activity {private linearlayout bigview; private view inflaterview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. background); bigview = (linearlayout) findviewbyid (R. id. bglayout); layoutinflater Inflater = layoutinflater. from (this); inflaterview = Inflater. inflate (R. layout. inflater2, null); bigview. addview (inflaterview, new layoutparams (layoutparams. match_parent, layoutparams. match_parent ));}}

You must set the permission in androidmanifest. xml.

 
   
   
    
    
     
      
       
       
      
     
     
     
     
     
    
   

Running effect:

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.