资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android的LayoutInflater

在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

publicabstractclass LayoutInflater extends Object 



获得 LayoutInflater 实例的三种方式

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);  



其实,这三种方式本质是相同的,从源码中可以看出:这三种方式最终本质是都是调用的Context.getSystemService()。

下面是一个Demo

公司主营业务:成都做网站、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出长沙免费做网站回馈大家。

  1. main.xml

  2. encoding="utf-8"?>     

  3. xmlns:android="http://schemas.android.com/apk/res/android"    

  4.     android:orientation="vertical"    

  5.     android:layout_width="fill_parent"    

  6.     android:layout_height="fill_parent"    

  7.     >     

  8.     android:layout_width="fill_parent"      

  9.     android:layout_height="wrap_content"      

  10.     android:text="@string/hello"    

  11.     />     

  12.     android:id="@+id/button"    

  13.     android:layout_width="wrap_content"    

  14.     android:layout_height="wrap_content"    

  15.     android:text="ShowCustomDialog"    

  16.     />     

  17.   

复制代码

定义对话框的布局方式custom_dialog.xml

  1. encoding="utf-8"?>     

  2. xmlns:android="http://schemas.android.com/apk/res/android"    

  3.               android:orientation="horizontal"    

  4.               android:layout_width="fill_parent"    

  5.               android:layout_height="fill_parent"    

  6.               android:padding="10dp"    

  7.               >     

  8.    

  9.                android:layout_width="wrap_content"    

  10.                android:layout_height="fill_parent"    

  11.                android:layout_marginRight="10dp"    

  12.                />     

  13.    

  14.               android:layout_width="wrap_content"    

  15.               android:layout_height="fill_parent"    

  16.               android:textColor="#FFF"    

  17.               />     

  18.    

复制代码

Activity代码

  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.       

  15. private Button button;  

  16.     public void onCreate(Bundle savedInstanceState) {  

  17.         super.onCreate(savedInstanceState);  

  18.         setContentView(R.layout.main);  

  19.           

  20.         button = (Button)findViewById(R.id.button);  

  21.         button.setOnClickListener(this);  

  22.     }  

  23. @Override 

  24. public void onClick(View v) {  

  25.     

  26.   showCustomDialog();  

  27. }  

  28.    

  29. public void showCustomDialog()  

  30. {  

  31.   AlertDialog.Builder builder;  

  32.   AlertDialog alertDialog;  

  33.   Context mContext = LayoutInflaterDemo.this;  

  34.     

  35.   //下面俩种方法都可以  

  36.   //LayoutInflater inflater = getLayoutInflater();  

  37.   LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  

  38.   View layout = inflater.inflate(R.layout.custom_dialog,null);  //返回值为view

  39.   TextView text = (TextView) layout.findViewById(R.id.text);  

  40.   text.setText("Hello, Welcome to Mr Wei's blog!");  

  41.   ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath);  

  42.   p_w_picpath.setImageResource(R.drawable.icon);  

  43.   builder = new AlertDialog.Builder(mContext);  

  44.   builder.setView(layout);  

  45.   alertDialog = builder.create();  

  46.   alertDialog.show();  

  47. }  

  48. }   

复制代码

运行效果:


Android的LayoutInflater


当前文章:Android的LayoutInflater
分享路径:http://cdkjz.cn/article/jeejsh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220