android layoutinflater用法

Inflater英文意思是膨胀,在android中大概是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而 findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
它的用法有2种:

1 LayoutInflater inflater = LayoutInflater.from( this );
2 View view=inflater.inflate(R.layout.ID, null );

或者干脆并成一句:

1 View view=LayoutInflater.from( this ).inflate(R.layout.ID, null );

另一种方法:

1 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
2 View view=inflater.inflate(R.layout.ID, null );

上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService():

1 public static LayoutInflater from(Context context) {
2      LayoutInflater LayoutInflater =
3              (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
4      if (LayoutInflater == null ) {
5          throw new AssertionError( "LayoutInflater not found." );
6      }
7      return LayoutInflater;
8 }

另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。

传入的Name返回的对象说明
WINDOW_SERVICEWindowManager管理打开的窗口程序
LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定义的view
ACTIVITY_SERVICEActivityManager管理应用程序的系统状态
POWER_SERVICEPowerManger电源的服务
ALARM_SERVICEAlarmManager闹钟的服务
NOTIFICATION_SERVICENotificationManager状态栏的服务
KEYGUARD_SERVICEKeyguardManager键盘锁的服务
LOCATION_SERVICELocationManager位置的服务,如GPS
SEARCH_SERVICESearchManager搜索的服务
VEBRATOR_SERVICEVebrator手机震动的服务
CONNECTIVITY_SERVICEConnectivity网络连接的服务
WIFI_SERVICEWifiManagerWi-Fi服务
TELEPHONY_SERVICETeleponyManager电话服务
01 //基本用法
02 public void showCustomDialog(){
03    AlertDialog.Builder builder;
04    AlertDialog alertDialog;
05    Context mContext = AppActivity. this ;
06 //下面俩种方法都可以
07    //LayoutInflater inflater = getLayoutInflater();
08    LayoutInflater inflater = (LayoutInflater)
09 mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
10    View layout = inflater.inflate(R.layout.custom_dialog, null );
11    TextView text = (TextView) layout.findViewById(R.id.text);
12    text.setText( "Hello, Welcome to Mr Wei's blog!" );
13    ImageView image = (ImageView) layout.findViewById(R.id.image);
14    image.setImageResource(R.drawable.icon);
15    builder = new AlertDialog.Builder(mContext);
16    builder.setView(layout);
17    alertDialog = builder.create();
18    alertDialog.show();
19   }
20 }
21  
22 protected void showToast( int type) {
23          Toast.makeText( this , "*********" , Toast.LENGTH_LONG).show(); 
24  
25          LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
26          View view = li.inflate(R.layout.toast, null ); 
27  
28          Toast toast = new Toast( this );
29          toast.setView(view);
30          toast.setDuration(type);
31          toast.show();
32      }

Android 动态加载布局

由于前段时间项目需要,需要在一个页面上加载根据不同的按钮加载不同的布局页面,当时想到用 tabhot 。不过美工提供的界面图完全用不上tabhot ,所以想到了动态加载的方法来解决这一需求。在这里我整理了一下,写了一个 DEMO 希望大家以后少走点弯路。

首先,我们先把界面的框架图画出来,示意图如下:

中间白色部门是一个线性布局文件,我喜欢在画图的时候用不同的颜色将一块布局标示出来,方便查看。布局文件代码如下:

01 <? xml version = "1.0" encoding = "utf-8" ?>
03      android:orientation="vertical" android:layout_width="fill_parent"
04      android:layout_height="fill_parent">
05      < LinearLayout android:orientation = "horizontal"
06          android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
07          < Button android:text = "加载ListView" android:id = "@+id/Button01"
08              android:layout_width = "wrap_content" android:layout_height = "wrap_content" >
09          </ Button >
10          < Button android:text = "加载另外一个页面<a href=" mailto:%22%20android:id=%22@+id/Button02">" android:id="@+id/Button02</ a >"
11              android:layout_width="wrap_content" android:layout_height="wrap_content"></ Button >
12      </ LinearLayout >
13      < LinearLayout android:id = "@+id/LinearLayout01" android:background = "#FFFFFF"
14          android:layout_width = "fill_parent" android:layout_height = "fill_parent" ></ LinearLayout >
15 </ LinearLayout >.

从上面的效果图可以看出,那块白色的线性布局是用来动态加载传进来的布局文件。好了,我们就来做如果把布局文件动态的加载进来。下面我们一步一步来实现这个效果,首先,先把需要的 XML 勾画出来,分为步骤如下。

新建一个布局用来存放 ListView 页面,代码如下:

1 <? xml version = "1.0" encoding = "UTF-8" ?>
2 < LinearLayout android:id = "@+id/layout"
3      android:layout_width = "fill_parent" android:layout_height = "fill_parent"
5      < ListView android:id = "@+id/ListView01" android:layout_width = "wrap_content"
6          android:layout_height = "wrap_content" ></ ListView >
7 </ LinearLayout >

新建一个 ListView 每一行数据的样式,代码如下:

1 <? xml version = "1.0" encoding = "UTF-8" ?>
2 < LinearLayout android:id = "@+id/LinearLayout01"
3      android:layout_width = "fill_parent" android:layout_height = "fill_parent"
5      < TextView android:text = "@+id/TextView01" android:id = "@+id/TextView01"
6          android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ TextView >
7 </ LinearLayout >

新建另外一个页面,用来区分此页面是动态加载的,代码如下:

1 <? xml version = "1.0" encoding = "UTF-8" ?>
2 < LinearLayout android:id = "@+id/hellolayout"
3      android:layout_width = "fill_parent" android:layout_height = "fill_parent"
5      < TextView android:text = "HELLO"  
6          android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ TextView >
7 </ LinearLayout >

实现ListView 的添充数据,这里不详细介绍如何填充ListView 每行数据,有不解的朋友可以回头看我写的文章:点击这里 ,代码如下:

01 package com.terry;
02 import java.util.ArrayList;
03 import java.util.HashMap;
04  
05 import android.content.Context;
06 import android.view.LayoutInflater;
07 import android.view.View;
08 import android.view.ViewGroup;
09 import android.widget.BaseAdapter;
10 import android.widget.TextView;
11  
12 public class listAdapter extends BaseAdapter {
13  
14      ArrayList> list = new ArrayList>();
15  
16      private LayoutInflater inflater;
17      public listAdapter(Context contex)
18      {
19          inflater=LayoutInflater.from(contex);
20          HashMap map= new HashMap();
21          for ( int i = 0 ; i < 10 ; i++) {
22              map.put( "name" , "例子" );
23              list.add(map);
24          }
25  
26      }
27  
28      @Override
29      public int getCount() {
30          // TODO Auto-generated method stub
31          return list.size();
32      }
33  
34      @Override
35      public Object getItem( int position) {
36          // TODO Auto-generated method stub
37          return list.get(position);
38      }
39  
40      @Override
41      public long getItemId( int position) {
42          // TODO Auto-generated method stub
43          return position;
44      }
45  
46      @Override
47      public View getView( int position, View convertView, ViewGroup parent) {
48          // TODO Auto-generated method stub
49          final viewHolder myHolder;
50          if (convertView== null ) {
51              myHolder= new viewHolder();
52              convertView=inflater.inflate(R.layout.list_view_row, null );
53              myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01);
54              convertView.setTag(myHolder);
55          }
56          else
57          {
58              myHolder=(viewHolder)convertView.getTag();
59          }
60          myHolder.tv.setText(list.get(position).get( "name" ).toString());
61          return convertView;
62      }
63  
64 }

项目大纲如下图:

好了,到此我们的准备工作就己经完成,接下来就是要教大家如何实现动态加载上面所画的布局页面了,先看一下效果图:

点击第一个按钮:

点击第二个按钮:

动态加载代码如下:

01 package com.terry;
02  
03 import android.app.Activity;
04 import android.graphics.Color;
05 import android.os.Bundle;
06 import android.view.LayoutInflater;
07 import android.view.View;
08 import android.view.View.OnClickListener;
09 import android.widget.Button;
10 import android.widget.LinearLayout;
11 import android.widget.ListView;
12 import android.widget.TextView;
13  
14 public class dynaActivity extends Activity {
15      /** Called when the activity is first created. */
16      @Override
17      public void onCreate(Bundle savedInstanceState) {
18          super .onCreate(savedInstanceState);
19          setContentView(R.layout.main);
20  
21          final LayoutInflater inflater = LayoutInflater.from( this );
22          Button btn = (Button) findViewById(R.id.Button01);
23          Button btn2 = (Button) findViewById(R.id.Button02);
24          final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
25          btn.setOnClickListener( new OnClickListener() {
26  
27              @Override
28              public void onClick(View v) {
29                  // TODO Auto-generated method stub
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值