layoutinflate应用
来源:互联网 发布:亚索腥红之月皮肤 知乎 编辑:程序博客网 时间:2024/05/16 18:52
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>
- layoutinflate应用
- LayoutInflate
- LayoutInflate
- Android LayoutInflate
- layoutInflate 解析
- Android - LayoutInflate用法
- Android - LayoutInflate用法
- 关于LayoutInflate的用法
- Android - LayoutInflate用法
- 【转】 Android - LayoutInflate用法
- LayoutInflate的使用
- Android - LayoutInflate用法
- 关于LayoutInflate的理解。
- LayoutInflate的使用
- 关于LayoutInflate详细解释
- LayoutInflate动态加载view
- Android LayoutInflate的基本知识
- Android LayoutInflate(一)
- flex 事件机制详解
- 用来解决 ViewFlipper 在其他控件上不能滑动的问题
- 深入理解string
- (转)Google Android 用户分析SDK
- MD5加密算法
- layoutinflate应用
- ping源代码完全解析
- 程序员不得不珍藏的那些励志片段
- 光耦介绍
- sql中的group by 和 having 用法解析
- android系统 连接管理总结
- C语言中#if #ifdef #if defined的区别
- UIButton setImage setTitle上同时显示图片和文字的方法(最有效)
- Windows 平台中透過 diff / patch 整合別人的 Code