【Android进阶学习】LayoutInflater的应用
2011-12-24 22:32
513 查看
LayoutInflater在Android中是“扩展”的意思,作用类似findViewById( ),它在Android开发中的作用是很大的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。
LayoutInflater与findViewById( )的不同点:
LayoutInflater是将XML中的Layout转换为View放入.java代码中
findViewById()是找具体xml下的具体组件(如:Button,TextView,ImageView等)。
获得LayoutInflater的三种方法:
第一种:
第二种:
第三种:
getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入 的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。其中LAYOUT_INFLATER_SERVICE返回的对象是 LayoutInflater,作用是取得XML定义的View。
第一个实例:
main.xml
MainActivity.java
效果图:
第二个实例:
下面的一个简单的LayoutInflater应用实例,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
main.xml
dialog.xml
MainActivity.java
效果图:
本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/750495
LayoutInflater与findViewById( )的不同点:
LayoutInflater是将XML中的Layout转换为View放入.java代码中
findViewById()是找具体xml下的具体组件(如:Button,TextView,ImageView等)。
获得LayoutInflater的三种方法:
第一种:
LayoutInflater inflater = LayoutInflater.from(this); View layout = inflater.inflate(R.layout.main, null);
第二种:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.main, null);
第三种:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.main, null);
getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入 的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。其中LAYOUT_INFLATER_SERVICE返回的对象是 LayoutInflater,作用是取得XML定义的View。
第一个实例:
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" > <EditText android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.lingdududu.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText etx;
private Button confirmBtn;
private Button cancleBtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.main, null);
etx = (EditText)layout.findViewById(R.id.text);
etx.setBackgroundColor(Color.WHITE);
etx.setHint("请输入你的学号");
confirmBtn = (Button)layout.findViewById(R.id.btn1);
confirmBtn.setText("确定");
cancleBtn = (Button)layout.findViewById(R.id.btn2);
cancleBtn.setText("取消");
setContentView(layout);
}
}
效果图:
第二个实例:
下面的一个简单的LayoutInflater应用实例,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
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/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="打开对话框" /> </LinearLayout>
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="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/dialog_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试LayoutInflater" /> </LinearLayout>
MainActivity.java
package com.lingdududu.test; import android.app.Activity; import android.app.AlertDialog; 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 MainActivity extends Activity { private Button showBtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showBtn = (Button)findViewById(R.id.btn); showBtn.setOnClickListener(new showDialogListener()); } class showDialogListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(); } } public void showDialog() { AlertDialog.Builder builder; AlertDialog dialog ; LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); //LayoutInflater inflater = getLayoutInflater(); //LayoutInflater inflater = LayoutInflater.from(this); View layout = inflater.inflate(R.layout.dialog, null); TextView tx = (TextView)layout.findViewById(R.id.dialog_txt); tx.setText("测试LayoutInflater"); ImageView img = (ImageView)layout.findViewById(R.id.img); img.setImageResource(R.drawable.icon); builder = new AlertDialog.Builder(this); builder.setView(layout); dialog = builder.create(); dialog.show(); } }
效果图:
本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/750495
相关文章推荐
- 【Android进阶学习】LayoutInflater的应用
- Android 进阶学习:Android LayoutInflater原理分析,带你一步步深入了解View(一)
- Android 进阶学习:Android LayoutInflater原理分析,带你一步步深入了解View(一)
- [转]Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android 中级应用 一 LayoutInflater 的使用 (动态加入view,让layout飞起来)
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用! (转)
- Android LayoutInflater原理分析,带你一步步深入了解View(一) ---站在巨人的肩膀上学习总结
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android LayoutInflater学习之 原理分析
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- android应用开发---(第1章)android基础学习之六大Layout布局
- Android学习之LayoutInflater类和inflate()方法的使用
- Android学习之LayoutInflater动态载入activity界面
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- android控件学习二:使用LayoutInflater动态加载布局和操作控件
- android学习关于LayoutInflater的使用