in the actual developmentLayoutinflaterThis class is also very useful, and it acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as Button, TextView, and so on) under the XML layout file.
Specific role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;
2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.
Layoutinflater is an abstract class that is declared in the document as follows:
publicabstractclass Layoutinflater extends Object
three ways to get Layoutinflater instances
1.LayoutInflater inflater = Getlayoutinflater (); Call activity's Getlayoutinflater ()
2.LayoutInflater localinflater = (layoutinflater) context.getsystemservice
(context.layout_inflater_service);
3. Layoutinflater Inflater = layoutinflater.from (context);
In fact, these three ways are essentially the same, as can be seen from the source code: The final essence of these three methods are called Context.getsystemservice ().
here is a demo
-
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"
-
>
-
<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>
Copy Code
define how dialog boxes are laid out Custom_dialog.xml
-
<?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>
Copy Code
Activity Code
-
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 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 Builder;
-
Alertdialog Alertdialog;
-
Context mcontext = layoutinflaterdemo.this;
-
-
Both of the following methods are available
-
Layoutinflater inflater = Getlayoutinflater ();
-
Layoutinflater Inflater = (layoutinflater) mcontext.getsystemservice (Layout_inflater_service);
-
View layout = inflater.inflate (r.layout.custom_dialog,null); return value is view
-
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 ();
-
}
-
}
-
Copy Code
Operating effect:
650) this.width=650; "id=" aimg_z0106 "class=" Zoom "src=" http://dl2.iteye.com/upload/attachment/0106/3124/ E4f099a4-5833-3ac8-95a7-a93f27bc91f4.png "border=" 0 "width=" 371 "height=" 346 "/>
Android's Layoutinflater