`

android:LayoutInflater详解

阅读更多
 在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。
  主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边ImageView,右边TextView)。


XML代码:

  <?xmlversion="1.0"encoding="utf-8"?>
  <LinearLayoutxmlns: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/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="ShowCustomDialog"/>
  
  </LinearLayout>

  定义对话框的布局方式,我们在layout目录下,新建一个名为custom_dialog.xml文件具体代码如下:
  


XML代码:

  <?xmlversion="1.0"encoding="utf-8"?>
  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp">
  
  <ImageView
  android:id="@+id/image"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginRight="10dp"/>
  
  <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:textColor="#FFF"/>
  
  </LinearLayout>

  主程序LayouInflaterDemo.java代码如下:
  
    packageEOE.android.Demo;
  importandroid.app.Activity;
  importandroid.app.AlertDialog;
  importandroid.content.Context;
  importandroid.os.Bundle;
  importandroid.view.LayoutInflater;
  importandroid.view.View;
  importandroid.view.View.OnClickListener;
  importandroid.widget.Button;
  importandroid.widget.ImageView;
  importandroid.widget.TextView;
  
  publicclassLayoutInflaterDemoextendsActivityimplementsOnClickListener{
  privateButtonbutton;
  
  publicvoidonCreate(BundlesavedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button=(Button)findViewById(R.id.button);
  button.setOnClickListener(this);
  }
  
  @Override
  publicvoidonClick(Viewv){
  showCustomDialog();
  }
  publicvoidshowCustomDialog(){
  AlertDialog.Builderbuilder;
  AlertDialogalertDialog;
  ContextmContext=LayoutInflaterDemo.this;
  
  //下面俩种方法都可以
  ////LayoutInflaterinflater=getLayoutInflater();
  
  LayoutInflaterinflater=(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);
  
  Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
  TextViewtext=(TextView)layout.findViewById(R.id.text);
  text.setText("Hello,WelcometoMrWei'sblog!");
  ImageViewimage=(ImageView)layout.findViewById(R.id.image);
  image.setImageResource(R.drawable.icon);
  builder=newAlertDialog.Builder(mContext);
  builder.setView(layout);
  alertDialog=builder.create();
  alertDialog.show();
  }
  
  }  
分享到:
评论

相关推荐

    Android LayoutInflater加载布局详解及实例代码

    Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...

    Android布局加载之LayoutInflater示例详解

    前言 Activity 在界面创建时需要将 XML 布局文件中的内容加载进来,正如我们在 ListView 或者 RecyclerView 中需要将 Item 的布局加载进来一样,都是使用 ...由于 Android 系统源码中关于 Content 部分采用的是装饰模

    Android开发中LayoutInflater用法详解

    本文实例讲述了Android开发中LayoutInflater用法。分享给大家供大家参考,具体如下: 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/...

    Android LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...

    Android中使用LayoutInflater要注意的一些坑

    LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...

    Android LayoutInflater.inflate源码分析

    LayoutInflater.inflate源码详解 LayoutInflater的inflate方法相信大家都不陌生,在Fragment的onCreateView中或者在BaseAdapter的getView方法中我们都会经常用这个方法来实例化出我们需要的View. 假设我们有一个...

    Android组件popupwindow使用方法详解

    先看效果:  现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的  上源码 main.xml &lt;?xml version=1.0 encoding=utf-8?... android:b

    Android实现在列表List中显示半透明小窗体效果的控件用法详解

    本文实例讲述了Android实现在列表List中显示半透明小窗体效果的控件用法。分享给大家供大家参考,具体如下: Android 在列表List中显示半透明...import android.view.LayoutInflater; import android.view.View; import

    android之SeekBar控件用法详解

    MainActivity.java ... import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app....import android.view.LayoutInflater; import android.view.M

    Android简易电话拨号器实例详解

    安卓开发简易电话拨号器,具体内容如下 我是基于安卓4.2.2开发的,下面是我写的MainActivity.java代码: ... import android.support.v7.app.ActionBarActivity;...import android.view.LayoutInflater; impo

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    Android getViewById和getLayoutInflater().inflate()的详解及比较  由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以...

    Android 自定义ListView示例详解

    本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView。 系统显示列表(ListView)时,首先会实例化一个适配器,本文...

    Android ListView里控件添加监听方法的实例详解

    Android ListView里控件添加监听方法的实例详解  关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现...

    详解Android PopupWindow怎么合理控制弹出位置(showAtLocation)

    说到PopupWindow,应该都会有种...View contentView = LayoutInflater.from(context).inflate(layoutId, null); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutP

Global site tag (gtag.js) - Google Analytics