LayoutInflate
来源:互联网 发布:sql如何授权用户 编辑:程序博客网 时间:2024/05/16 01:37
LayoutInflater 在 android 开发中使用频率较高,今天谈谈!
该类是一个抽象类,在文档中如下声明:
- public abstract class LayoutInflater extends Object
三种方法可以获得该实例对象,方法如下:
- a. LayoutInflater inflater = getLayoutInflater();
- b. LayoutInflater localinflater =
- (LayoutInflater)context.getSystemService
- (Context.LAYOUT_INFLATER_SERVICE);
- c. LayoutInflater inflater = LayoutInflater.from(context);
getLayoutInflater()方法,那么,分享一下该源代码:
- public PhoneWindow(Context context) {
- super(context);
- mLayoutInflater = LayoutInflater.from(context);
- }<span style="font-family: Arial, Verdana, sans-serif; white-space: normal; "> </span>
- /**
- * Obtains the LayoutInflater from the given context.
- */
- public static LayoutInflater from(Context context) {
- LayoutInflater LayoutInflater =
- (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (LayoutInflater == null) {
- throw new AssertionError("LayoutInflater not found.");
- }
- return LayoutInflater;
- }
inflate 愿意是充气之类的,在这里主要意思就是,扩张、使之膨胀。换句话说就是将当前视图view 补充完整、扩展该视图。
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
- public View inflate (int resource, ViewGroup root)
- public View inflate (XmlPullParser parser, ViewGroup root)
- public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
- public View inflate (int resource, ViewGroup root, boolean attachToRoot)
- LayoutInflater inflater = (LayoutInflater)
- getSystemService(LAYOUT_INFLATER_SERVICE);
- /* R.id.test 是 custom.xml 中根(root)布局 LinearLayout 的 id */
- View view = inflater.inflate(R.layout.custom,
- (ViewGroup)findViewById(R.id.test));
- /* 通过该 view 实例化 EditText对象, 否则报错,因为当前视图不是custom.xml.即没有 setContentView(R.layout.custom) 或者 addView() */
- //EditText editText = (EditText)findViewById(R.id.content);// error
- EditText editText = (EditText)view.findViewById(R.id.content);
注意:该方法与 findViewById 方法不同。inflater 是用来找 layout 下 xml 布局文件,并且实例化!而 findViewById() 是找具体 xml 下的具体 widget 控件(如:Button,TextView 等)。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
(0)她可以有很多地方可以使用,如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。
它的用法有2种:
Java代码
- LayoutInflater inflater = LayoutInflater.from(this);
- View view=inflater.inflate(R.layout.ID, null);
- 或者干脆并成一句:
- View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
另一种方法:
Java代码
- LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
- View view=inflater.inflate(R.layout.ID, null);
上面2种方法本质上是一样的,看下面的源码,form()调用的就是getSystemService():
Java代码
- Java代码
- public static LayoutInflater from(Context context) {
- LayoutInflater LayoutInflater =
- (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (LayoutInflater == null) {
- throw new AssertionError("LayoutInflater not found.");
- }
- return LayoutInflater;
- }
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
传入的Name返回的对象说明WINDOW_SERVICEWindowManager管理打开的窗口程序LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定义的viewACTIVITY_SERVICEActivityManager管理应用程序的系统状态POWER_SERVICEPowerManger电源的服务ALARM_SERVICEAlarmManager闹钟的服务NOTIFICATION_SERVICENotificationManager状态栏的服务KEYGUARD_SERVICEKeyguardManager键盘锁的服务LOCATION_SERVICELocationManager位置的服务,如GPSSEARCH_SERVICESearchManager搜索的服务VEBRATOR_SERVICEVebrator手机震动的服务CONNECTIVITY_SERVICEConnectivity网络连接的服务WIFI_SERVICEWifiManagerWi-Fi服务TELEPHONY_SERVICETeleponyManager电话服务
Java代码
- Java代码
- //基本用法
- public void showCustomDialog(){
- AlertDialog.Builder builder;
- AlertDialog alertDialog;
- Context mContext = AppActivity.this;
- //下面俩种方法都可以
- //LayoutInflater inflater = getLayoutInflater();
- LayoutInflater inflater = (LayoutInflater)
- mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
- View layout = inflater.inflate(R.layout.custom_dialog,null);
- 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();
- }
- }
- protected void showToast(int type) {
- Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
- LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = li.inflate(R.layout.toast, null);
- Toast toast = new Toast(this);
- toast.setView(view);
- toast.setDuration(type);
- toast.show();
- }
- LayoutInflate
- LayoutInflate
- Android LayoutInflate
- layoutInflate 解析
- layoutinflate应用
- Android - LayoutInflate用法
- Android - LayoutInflate用法
- 关于LayoutInflate的用法
- Android - LayoutInflate用法
- 【转】 Android - LayoutInflate用法
- LayoutInflate的使用
- Android - LayoutInflate用法
- 关于LayoutInflate的理解。
- LayoutInflate的使用
- 关于LayoutInflate详细解释
- LayoutInflate动态加载view
- Android LayoutInflate的基本知识
- Android LayoutInflate(一)
- windows utf8 签名 一个很二的问题(经典乱码“锘”字)
- C语言关键字volatile的使用
- 2011程序员薪资调查报告全文发布
- 程序员技术练级攻略 [转载]
- C/C++中time.h
- LayoutInflate
- SAP NetWeaver TH_GREP module - Code injection vulnerability (NEW)
- sqlldr direct参数小测试
- php常用比较函数区别表
- LINUX AIX HPUX 配置DNS域名解析
- 解决IE5、IE6、IE7与W3C标准的冲突,使用(IE7.js IE8.js)兼容
- Android安装应用程序出现“INSTALL_FAILED_CONTAINER_ERROR”错误的解决办法
- Java注释@interface的用法(未读)
- java中使用LinkedList实现stack机制