基于Android LayoutInflater的使用介绍

 更新时间:2013年04月19日 17:03:33   作者:  
本篇文章小编为大家介绍,基于Android LayoutInflater的使用介绍。需要的朋友参考下

在android中,LayoutInflater有点类似于Activity的findViewById(id),不同的是LayoutInflater是用来找layout下的xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

下面通过一个例子进行详细说明:

1、在res/layout文件夹下,添加一个xml文件dialog.xml

复制代码 代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" >

<ImageView
  android:id="@+id/diaimage"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent" >
</ImageView>

<TextView
  android:id="@+id/diatv"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent" />

</LinearLayout>


2、在main.xml文件中添加一个按钮,此按钮用于实现点击显示一个Dialog
复制代码 代码如下:

<Button
android:id="@+id/btnshowdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />

3、在MainActivity的onCreate方法中添加如下代码,实现具体功能操作
复制代码 代码如下:

  Button showdialog = (Button) findViewById(R.id.btnshowdialog);
  showdialog.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    AlertDialog dialog;
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.dialog, null);

    TextView diatv = (TextView) layout.findViewById(R.id.diatv);
    diatv.setText("Welcome to LayoutInflater study");
    ImageView image = (ImageView) layout.findViewById(R.id.diaimage);
    image.setImageResource(R.drawable.ic_launcher);

    builder.setView(layout);// <--important,设置对话框内容的View
    dialog = builder.create();
    dialog.show();
   }
  });


运行程序,点击按钮,将实现如下效果!

相关文章

  • Android 仿微信小程序入口动画

    Android 仿微信小程序入口动画

    突然发现微信下拉小程序入口动画非常细腻,比较好奇,所以仿照他做了一个,并不是很完美,部分效果还没完成,但总体自我感觉还不错,效果见下文
    2021-06-06
  • mac开发android环境搭建步骤图解

    mac开发android环境搭建步骤图解

    这里比较详细的来总结下mac开发android的环境搭建步骤安装过程,希望对一些正准备配置Android开发环境的小伙伴们有一定帮助
    2014-01-01
  • 详解Gradle构建过程

    详解Gradle构建过程

    Gradle是项目构建工具,是Google官方推荐的Android项目编译工具。构建工具是可以让开发者以可执行和有序的任务来表达自动化的需求。就是将源代码生成可执行程序。本文将详细介绍Gradle构建过程
    2021-06-06
  • Android仿字节颜色自定义进度条

    Android仿字节颜色自定义进度条

    这篇文章主要为大家详细介绍了Android仿字节颜色自定义进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 深入Android开发FAQ的详解

    深入Android开发FAQ的详解

    本篇文章是对Android开发FAQ进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android中ProgressBar用法简单实例

    Android中ProgressBar用法简单实例

    这篇文章主要介绍了Android中ProgressBar用法,以简单实例形式分析了Android中ProgressBar进度条控件的功能与布局相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android N多窗口支持

    Android N多窗口支持

    Android N 可以同时显示多个应用窗口。在手机上,两个应用可以在“分屏”模式中左右并排或上下并排显示。本文将对此介绍。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-05-05
  • 深入android中The connection to adb is down的问题以及解决方法

    深入android中The connection to adb is 

    本篇文章是对android中The connection to adb is down的问题以及解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android无限循环RecyclerView的完美实现方案

    Android无限循环RecyclerView的完美实现方案

    这篇文章主要介绍了Android无限循环RecyclerView的完美实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • android自定义手表效果

    android自定义手表效果

    这篇文章主要为大家详细介绍了android自定义手表效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02

最新评论