zoukankan      html  css  js  c++  java
  • LayoutInflater skyCc

    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

    2 5 10111213 复制代码 dialog.xml布局文件 1

    2 7 8 9 10 复制代码 演示结果如下: 默认dialog: 自定义dialog: 自定义toast:

  • 相关阅读:
    JDK1.8HashMap底层实现原理
    关于map转json,空key丢失的问题
    spring一些注解的使用及相关注解差异
    搭建基础项目遇到的一些小坑
    解析ftp上word文档的文字并输入
    R语言中回归模型预测的不同类型置信区间应用比较分析
    R语言中的广义线性模型(GLM)和广义相加模型(GAM):多元(平滑)回归分析保险资金投资组合信用风险敞口
    R语言对巨灾风险下的再保险合同定价研究案例:广义线性模型和帕累托分布Pareto distributions分析
    R语言中GLM(广义线性模型),非线性和异方差可视化分析
    如何用R语言绘制生成正态分布图表
  • 原文地址:https://www.cnblogs.com/cmzcheng/p/2344514.html
Copyright © 2011-2022 走看看