Chinaunix首页 | 论坛 | 博客
  • 博客访问: 285919
  • 博文数量: 68
  • 博客积分: 1474
  • 博客等级: 上尉
  • 技术积分: 616
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 12:07
文章分类

全部博文(68)

文章存档

2011年(68)

分类: 嵌入式

2011-02-16 09:24:59

Inflater英文意思是膨胀,在android中大概是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而 findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
它的用法有2种:
Java代码
  1. 1. LayoutInflater inflater = LayoutInflater.from(this);
  2.    2. View view=inflater.inflate(R.layout.ID, null);
  3.    3. 或者干脆并成一句:
  4.    4. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
  5. LayoutInflater inflater = LayoutInflater.from(this);
  6. View view=inflater.inflate(R.layout.ID, null);

或者干脆并成一句:
  1. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
 
另一种方法:
Java代码
  1. 1. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  2. 2. View view=inflater.inflate(R.layout.ID, null);
  3. LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
  4. View view=inflater.inflate(R.layout.ID, null);
 
上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService():
Java代码
  1. 1. public static LayoutInflater from(Context context) {
  2.    2. LayoutInflater LayoutInflater =
  3.    3. (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  4.    4. if (LayoutInflater == null) {
  5.    5. throw new AssertionError("LayoutInflater not found.");
  6.    6. }
  7.    7. return LayoutInflater;
  8.    8. }
  9. public static LayoutInflater from(Context context) {
  10.     LayoutInflater LayoutInflater =
  11.             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  12.     if (LayoutInflater == null) {
  13.         throw new AssertionError("LayoutInflater not found.");
  14.     }
  15.     return LayoutInflater;
  16. }

另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name  返回的对象  说明
WINDOW_SERVICE  WindowManager  管理打开的窗口程序
LAYOUT_INFLATER_SERVICE  LayoutInflater  取得xml里定义的view
ACTIVITY_SERVICE  ActivityManager  管理应用程序的系统状态
POWER_SERVICE PowerManger  电源的服务
ALARM_SERVICE  AlarmManager  闹钟的服务
NOTIFICATION_SERVICE  NotificationManager  状态栏的服务
KEYGUARD_SERVICE  KeyguardManager  键盘锁的服务
LOCATION_SERVICE  LocationManager  位置的服务,如GPS
SEARCH_SERVICE  SearchManager  搜索的服务
VEBRATOR_SERVICE  Vebrator  手机震动的服务
CONNECTIVITY_SERVICE  Connectivity  网络连接的服务
WIFI_SERVICE  WifiManager  Wi-Fi服务
TELEPHONY_SERVICE  TeleponyManager  电话服务
 
Java代码
  1. 1. //基本用法

  2.    2. public void showCustomDialog(){
  3.    3. AlertDialog.Builder builder;
  4.    4. AlertDialog alertDialog;
  5.    5. Context mContext = AppActivity.this;
  6.    6. //下面俩种方法都可以

  7.    7. //LayoutInflater inflater = getLayoutInflater();

  8.    8. LayoutInflater inflater = (LayoutInflater)
  9.    9. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  10.   10. View layout = inflater.inflate(R.layout.custom_dialog,null);
  11.   11. TextView text = (TextView) layout.findViewById(R.id.text);
  12.   12. text.setText("Hello, Welcome to Mr Wei's blog!");
  13.   13. ImageView image = (ImageView) layout.findViewById(R.id.image);
  14.   14. image.setImageResource(R.drawable.icon);
  15.   15. builder = new AlertDialog.Builder(mContext);
  16.   16. builder.setView(layout);
  17.   17. alertDialog = builder.create();
  18.   18. alertDialog.show();
  19.   19. }
  20.   20. }
  21.   21.
  22.   22. protected void showToast(int type) {
  23.   23. Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
  24.   24.
  25.   25. LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  26.   26. View view = li.inflate(R.layout.toast, null);
  27.   27.
  28.   28. Toast toast = new Toast(this);
  29.   29. toast.setView(view);
  30.   30. toast.setDuration(type);
  31.   31. toast.show();
  32.   32. }
  33. //基本用法

  34. public void showCustomDialog(){
  35.   AlertDialog.Builder builder;
  36.   AlertDialog alertDialog;
  37.   Context mContext = AppActivity.this;
  38. //下面俩种方法都可以

  39.   //LayoutInflater inflater = getLayoutInflater();

  40.   LayoutInflater inflater = (LayoutInflater)
  41. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
  42.   View layout = inflater.inflate(R.layout.custom_dialog,null);
  43.   TextView text = (TextView) layout.findViewById(R.id.text);
  44.   text.setText("Hello, Welcome to Mr Wei's blog!");
  45.   ImageView image = (ImageView) layout.findViewById(R.id.image);
  46.   image.setImageResource(R.drawable.icon);
  47.   builder = new AlertDialog.Builder(mContext);
  48.   builder.setView(layout);
  49.   alertDialog = builder.create();
  50.   alertDialog.show();
  51.  }
  52. }
  53. protected void showToast(int type) {
  54.         Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
  55.   
  56.         LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  57.         View view = li.inflate(R.layout.toast, null);
  58.           
  59.         Toast toast = new Toast(this);
  60.         toast.setView(view);
  61.         toast.setDuration(type);
  62.         toast.show();
  63.     }

Android 动态加载布局
http://labs.chinamobile.com/mblog/532767_72588?fdlayenxoaencysxyant
由于前段时间项目需要,需要在一个页面上加载根据不同的按钮加载不同的布局页面,当时想到用 tabhot 。不过美工提供的界面图完全用不上tabhot ,所以想到了动态加载的方法来解决这一需求。在这里我整理了一下,写了一个 DEMO 希望大家以后少走点弯路。
首先,我们先把界面的框架图画出来,示意图如下:
 
中间白色部门是一个线性布局文件,我喜欢在画图的时候用不同的颜色将一块布局标示出来,方便查看。布局文件代码如下:
Xml代码
 
  1. 1. <?xml version="1.0" encoding="utf-8"?>
  2.    2. <LinearLayout xmlns:android=""
  3.    3. android:orientation="vertical" android:layout_width="fill_parent"
  4.    4. android:layout_height="fill_parent">
  5.    5.
  6.    6.
  7.    7. <LinearLayout android:orientation="horizontal"
  8.    8. android:layout_width="wrap_content" android:layout_height="wrap_content">
  9.    9. <Button android:text="加载ListView" android:id="@+id/Button01"
  10.   10. android:layout_width="wrap_content" android:layout_height="wrap_content">
  11.   11. </Button>
  12.   12. <Button android:text="加载另外一个页面" android:id="@+id/Button02"
  13.   13. android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  14.   14. </LinearLayout>
  15.   15. <LinearLayout android:id="@+id/LinearLayout01" android:background="#FFFFFF"
  16.   16. android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout>
  17.   17. </LinearLayout>
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <LinearLayout xmlns:android=""
  20.     android:orientation="vertical" android:layout_width="fill_parent"
  21.     android:layout_height="fill_parent">

  22.     <LinearLayout android:orientation="horizontal"
  23.         android:layout_width="wrap_content" android:layout_height="wrap_content">
  24.         <Button android:text="加载ListView" android:id="@+id/Button01"
  25.             android:layout_width="wrap_content" android:layout_height="wrap_content">
  26.         </Button>
  27.         <Button android:text="加载另外一个页面" android:id="@+id/Button02"
  28.             android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  29.     </LinearLayout>
  30.     <LinearLayout android:id="@+id/LinearLayout01" android:background="#FFFFFF"
  31.         android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout>
  32. </LinearLayout>

从上面的效果图可以看出,那块白色的线性布局是用来动态加载传进来的布局文件。好了,我们就来做如果把布局文件动态的加载进来。下面我们一步一步来实现这个效果,首先,先把需要的 XML  勾画出来,分为步骤如下。
新建一个布局用来存放 ListView 页面,代码如下:
Xml代码
 
  1. 1. <?xml version="1.0" encoding="UTF-8"?>
  2.    2. <LinearLayout android:id="@+id/layout"
  3.    3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.    4. xmlns:android="">
  5.    5. <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
  6.    6. android:layout_height="wrap_content"></ListView>
  7.    7. </LinearLayout>
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <LinearLayout android:id="@+id/layout"
  10.     android:layout_width="fill_parent" android:layout_height="fill_parent"
  11.     xmlns:android="">
  12.     <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"></ListView>
  14. </LinearLayout>

新建一个 ListView 每一行数据的样式,代码如下:
Xml代码
 
  1. 1. <?xml version="1.0" encoding="UTF-8"?>
  2.    2. <LinearLayout android:id="@+id/LinearLayout01"
  3.    3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.    4. xmlns:android="">
  5.    5. <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
  6.    6. android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
  7.    7. </LinearLayout>
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <LinearLayout android:id="@+id/LinearLayout01"
  10.     android:layout_width="fill_parent" android:layout_height="fill_parent"
  11.     xmlns:android="">
  12.     <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
  13.         android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
  14. </LinearLayout>

新建另外一个页面,用来区分此页面是动态加载的,代码如下:
Xml代码
  1. 1. <?xml version="1.0" encoding="UTF-8"?>
  2.    2. <LinearLayout android:id="@+id/hellolayout"
  3.    3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.    4. xmlns:android="">
  5.    5. <TextView android:text="HELLO"
  6.    6. android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
  7.    7. </LinearLayout>
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <LinearLayout android:id="@+id/hellolayout"
  10.     android:layout_width="fill_parent" android:layout_height="fill_parent"
  11.     xmlns:android="">
  12.     <TextView android:text="HELLO"
  13.         android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
  14. </LinearLayout>
实现ListView 的添充数据,这里不详细介绍如何填充ListView 每行数据,有不解的朋友可以回头看我写的文章:点击这里 ,代码如下:
Java代码
  1. 1. package com.terry;
  2.    2.
  3.    3. import java.util.ArrayList;
  4.    4. import java.util.HashMap;
  5.    5.
  6.    6. import android.content.Context;
  7.    7. import android.view.LayoutInflater;
  8.    8. import android.view.View;
  9.    9. import android.view.ViewGroup;
  10.   10. import android.widget.BaseAdapter;
  11.   11. import android.widget.TextView;
  12.   12.
  13.   13. public class listAdapter extends BaseAdapter {
  14.   14.
  15.   15. ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
  16.   16.
  17.   17. private LayoutInflater inflater;
  18.   18. public listAdapter(Context contex)
  19.   19. {
  20.   20. inflater=LayoutInflater.from(contex);
  21.   21. HashMap<String, Object> map=new HashMap<String, Object>();
  22.   22. for (int i = 0; i < 10; i++) {
  23.   23. map.put("name", "例子");
  24.   24. list.add(map);
  25.   25. }
  26.   26.
  27.   27. }
  28.   28.
  29.   29. @Override
  30.   30. public int getCount() {
  31.   31. // TODO Auto-generated method stub

  32.   32. return list.size();
  33.   33. }
  34.   34.
  35.   35. @Override
  36.   36. public Object getItem(int position) {
  37.   37. // TODO Auto-generated method stub

  38.   38. return list.get(position);
  39.   39. }
  40.   40.
  41.   41. @Override
  42.   42. public long getItemId(int position) {
  43.   43. // TODO Auto-generated method stub

  44.   44. return position;
  45.   45. }
  46.   46.
  47.   47. @Override
  48.   48. public View getView(int position, View convertView, ViewGroup parent) {
  49.   49. // TODO Auto-generated method stub

  50.   50. final viewHolder myHolder;
  51.   51. if (convertView==null) {
  52.   52. myHolder=new viewHolder();
  53.   53. convertView=inflater.inflate(R.layout.list_view_row, null);
  54.   54. myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01);
  55.   55. convertView.setTag(myHolder);
  56.   56. }
  57.   57. else
  58.   58. {
  59.   59. myHolder=(viewHolder)convertView.getTag();
  60.   60. }
  61.   61. myHolder.tv.setText(list.get(position).get("name").toString());
  62.   62. return convertView;
  63.   63. }
  64.   64.
  65.   65. }
  1. package com.terry;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. public class listAdapter extends BaseAdapter {
  11.     ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
  12.     private LayoutInflater inflater;
  13.     public listAdapter(Context contex)
  14.     {
  15.         inflater=LayoutInflater.from(contex);
  16.         HashMap<String, Object> map=new HashMap<String, Object>();
  17.         for (int i = 0; i < 10; i++) {
  18.             map.put("name", "例子");
  19.             list.add(map);
  20.         }
  21.         
  22.     }
  23.     
  24.     @Override
  25.     public int getCount() {
  26.         // TODO Auto-generated method stub

  27.         return list.size();
  28.     }
  29.     @Override
  30.     public Object getItem(int position) {
  31.         // TODO Auto-generated method stub

  32.         return list.get(position);
  33.     }
  34.     @Override
  35.     public long getItemId(int position) {
  36.         // TODO Auto-generated method stub

  37.         return position;
  38.     }
  39.     @Override
  40.     public View getView(int position, View convertView, ViewGroup parent) {
  41.         // TODO Auto-generated method stub

  42.         final viewHolder myHolder;
  43.         if (convertView==null) {
  44.             myHolder=new viewHolder();
  45.             convertView=inflater.inflate(R.layout.list_view_row, null);
  46.             myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01);
  47.             convertView.setTag(myHolder);
  48.         }
  49.         else
  50.         {
  51.             myHolder=(viewHolder)convertView.getTag();
  52.         }
  53.         myHolder.tv.setText(list.get(position).get("name").toString());
  54.         return convertView;
  55.     }
  56. }

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

点击第二个按钮:
 

动态加载代码如下:
Java代码
  1. 1. package com.terry;
  2.    2.
  3.    3. import android.app.Activity;
  4.    4. import android.graphics.Color;
  5.    5. import android.os.Bundle;
  6.    6. import android.view.LayoutInflater;
  7.    7. import android.view.View;
  8.    8. import android.view.View.OnClickListener;
  9.    9. import android.widget.Button;
  10.   10. import android.widget.LinearLayout;
  11.   11. import android.widget.ListView;
  12.   12. import android.widget.TextView;
  13.   13.
  14.   14. public class dynaActivity extends Activity {
  15.   15. /** Called when the activity is first created. */
  16.   16. @Override
  17.   17. public void onCreate(Bundle savedInstanceState) {
  18.   18. super.onCreate(savedInstanceState);
  19.   19. setContentView(R.layout.main);
  20.   20.
  21.   21. final LayoutInflater inflater = LayoutInflater.from(this);
  22.   22. Button btn = (Button) findViewById(R.id.Button01);
  23.   23. Button btn2 = (Button) findViewById(R.id.Button02);
  24.   24. final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
  25.   25. btn.setOnClickListener(new OnClickListener() {
  26.   26.
  27.   27. @Override
  28.   28. public void onClick(View v) {
  29.   29. // TODO Auto-generated method stub

  30.   30. LinearLayout layout = (LinearLayout) inflater.inflate(
  31.   31. R.layout.listview, null).findViewById(R.id.layout);
  32.   32. ListView lv=(ListView)layout.getChildAt(0);
  33.   33. lv.setAdapter(new listAdapter(dynaActivity.this));
  34.   34. lin.removeAllViews();
  35.   35. lin.addView(layout);
  36.   36. }
  37.   37. });
  38.   38.
  39.   39. btn2.setOnClickListener(new OnClickListener() {
  40.   40.
  41.   41. @Override
  42.   42. public void onClick(View v) {
  43.   43. // TODO Auto-generated method stub

  44.   44. LinearLayout layout = (LinearLayout) inflater.inflate(
  45.   45. R.layout.hello, null).findViewById(R.id.hellolayout);
  46.   46. TextView lv=(TextView)layout.getChildAt(0);
  47.   47. lv.setTextColor(Color.RED);
  48.   48. lin.removeAllViews();
  49.   49. lin.addView(layout);
  50.   50. }
  51.   51. });
  52.   52. }
  53.   53. }
  1. package com.terry;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.LinearLayout;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. public class dynaActivity extends Activity {
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.         final LayoutInflater inflater = LayoutInflater.from(this);
  19.         Button btn = (Button) findViewById(R.id.Button01);
  20.         Button btn2 = (Button) findViewById(R.id.Button02);
  21.         final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
  22.         btn.setOnClickListener(new OnClickListener() {
  23.             @Override
  24.             public void onClick(View v) {
  25.                 // TODO Auto-generated method stub

  26.                 LinearLayout layout = (LinearLayout) inflater.inflate(
  27.                         R.layout.listview, null).findViewById(R.id.layout);
  28.                 ListView lv=(ListView)layout.getChildAt(0);
  29.                 lv.setAdapter(new listAdapter(dynaActivity.this));
  30.                 lin.removeAllViews();
  31.                 lin.addView(layout);
  32.             }
  33.         });
  34.         
  35.         btn2.setOnClickListener(new OnClickListener() {
  36.             
  37.             @Override
  38.             public void onClick(View v) {
  39.                 // TODO Auto-generated method stub

  40.                 LinearLayout layout = (LinearLayout) inflater.inflate(
  41.                         R.layout.hello, null).findViewById(R.id.hellolayout);
  42.                  TextView lv=(TextView)layout.getChildAt(0);
  43.                  lv.setTextColor(Color.RED);
  44.                 lin.removeAllViews();
  45.                 lin.addView(layout);
  46.             }
  47.         });
  48.     }
  49. }

上面通过使用LayoutInflater  每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里面的要放的布局文件里面,每次动态加载文件必需 调用 removeAllViews方法,清除之前的加载进来的 View 。是不是很简单?当然动态加载VIEW 还有许多种方法,多尝试不同写法。可能会领会不一样的心得,祝你早上掌握android 的开发技术。
Tip:因为是基于VIEW 操作,因此你可以用 Animation 的动画效果使其更换界面更为自然,观赏性更强。
阅读(2475) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~