栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

基于Android LayoutInflater的使用介绍

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于Android LayoutInflater的使用介绍

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

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

1、在res/layout文件夹下,添加一个xml文件dialog.xml
复制代码 代码如下:
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" >

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

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



2、在main.xml文件中添加一个按钮,此按钮用于实现点击显示一个Dialog
复制代码 代码如下:
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();
   }
  });

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/165481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号