LayoutInflater的使用
来源:互联网 发布:sql if else 循环 编辑:程序博客网 时间:2024/04/27 10:13
LayoutInflater的作用和findViewById()差不多,不同之处在于LayoutInflater是找layout文件夹下的某个xml布局文件!而 findViewById()是找某一个xml下的具体 widget控件。
LayoutInflater可以将一个xml布局文件转化为View 类型,其代码如下:
View Code
1 package hzx.one; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.app.AlertDialog.Builder; 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.Button;12 import android.widget.Toast;13 14 public class HzxDialogActivity extends Activity {15 private Button button1;16 private Button button2;17 private Button button3;18 @Override19 public void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.main);22 button1=(Button) findViewById(R.id.button1);23 button1.setText("默认dialog");24 button1.setOnClickListener(new View.OnClickListener() {25 @Override26 public void onClick(View v) {showDialog1();}});27 button2=(Button) findViewById(R.id.button2);28 button2.setText("自定义dialog");29 button2.setOnClickListener(new View.OnClickListener() {30 @Override31 public void onClick(View v) {showDialog2();}});32 button3=(Button) findViewById(R.id.button3);33 button3.setText("自定义toast");34 button3.setOnClickListener(new View.OnClickListener() {35 @Override36 public void onClick(View v) {37 Toast toast=new Toast(HzxDialogActivity.this);38 //三种方法定义LayoutInflater39 //LayoutInflater li=getLayoutInflater();40 //LayoutInflater li = LayoutInflater.from(HzxDialogActivity.this); 41 LayoutInflater li = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);42 43 View view=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));44 toast.setView(view);45 toast.setDuration(Toast.LENGTH_SHORT);46 toast.setGravity(0, -20, 20);47 toast.show();48 }});49 }50 private void showDialog1() {51 AlertDialog.Builder builder=new Builder(this);52 builder.setMessage("are you sure to exit ?");53 builder.setTitle("exit or not ");54 builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {55 @Override56 public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});57 builder.setNegativeButton("no", new DialogInterface.OnClickListener() {58 @Override59 public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});60 builder.create().show();61 }62 private void showDialog2(){63 AlertDialog.Builder builder=new Builder(this);64 LayoutInflater li=getLayoutInflater();65 View v=li.inflate(R.layout.dialog, (ViewGroup)findViewById(R.id.dialog));66 builder.setView(v);67 builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {68 @Override69 public void onClick(DialogInterface dialog, int which) {dialog.dismiss();HzxDialogActivity.this.finish();}});70 builder.setNegativeButton("no", new DialogInterface.OnClickListener() {71 @Override72 public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});73 builder.create().show();74 }75 }
main.xml布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" android:orientation="vertical"> 5 <TextView 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="@string/hello" 9 />10 <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>11 <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>12 <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>13 </LinearLayout>
dialog.xml布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="horizontal" 6 android:id="@+id/dialog"> 7 8 <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView> 9 <TextView android:layout_height="wrap_content" android:text="@string/hello" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>10 </LinearLayout>
演示结果如下:
默认dialog:
自定义dialog:
自定义toast:
- LayoutInflater---LayoutInflater的使用
- 【LayoutInflater】Android 中LayoutInflater的使用!
- LayoutInflater的使用,inflate
- LayoutInflater的使用
- android LayoutInflater的使用
- LayoutInflater的使用
- LayoutInflater 的使用
- Android - LayoutInflater 的使用
- LayoutInflater的使用
- LayoutInflater的使用,inflate
- LAYOUTINFLATER的使用
- LAYOUTINFLATER的使用
- LayoutInflater的使用
- LayoutInflater的使用
- LayoutInflater的使用
- LayoutInflater的使用
- LayoutInflater的使用
- LayoutInflater的使用,inflate
- linux下提示bash:command not found
- QT 开发实战
- 数据备份形式主要方式解读大全
- px,in,mm,pt,dp,dip,sp 之间的换算公式以及区别
- 上传图片并创建相应文件夹
- LayoutInflater的使用
- Java获得当前类所在路径
- windows 2003 IIS 服务器设置 301重定向 出现 eurl.axd
- C++引用本质
- android ndk 入门之打印log信息
- Win32 and COM for Metro style apps (data)
- [C++] 应该如何应对OOM?
- MTK相关文章
- VC实现wav录音文件的播放