`
caiwb1990
  • 浏览: 312569 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

LayoutInflater的使用

阅读更多
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),

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

一个简单的Demo,主布局main.xml里有一个TextView和一个Button,
当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

1、新建一个 Android工程,我们命名为LayoutInflaterDemo.


2、修改main.xml布局,里面主要在原来基础上增加了一个Button.代码如下:

<?xml version="1.0"   
encoding="utf-8"?>  
<LinearLayout   
xmlns: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> 


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

<?xml version="1.0"   
encoding="utf-8"?>  
<LinearLayout   
xmlns: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> 


4.修改主程序LayouInflaterDemo.java代码如下
    package cn.caiwb.inflater;  
    import android.app.Activity;  
    import android.app.AlertDialog;  
    import android.content.Context;  
    import android.os.Bundle;  
    import android.view.LayoutInflater;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.ImageView;  
    import android.widget.TextView;  
    public class LayoutInflaterDemo extends Activity implements   
    OnClickListener {  
          
        private Button button;  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
              
            button = (Button)findViewById(R.id.button);  
            button.setOnClickListener(this);  
        }  
        @Override  
        public void onClick(View v) {  
              
            showCustomDialog();  
        }  
          
        public void showCustomDialog()  
        {  
            AlertDialog.Builder builder;  
            AlertDialog alertDialog;  
            Context mContext = LayoutInflaterDemo.this;  
              
            //下面俩种方法都可以  
            ////LayoutInflater inflater = getLayoutInflater();  
            LayoutInflater inflater = (LayoutInflater)   
    mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
            View layout = inflater.inflate(R.layout.custom_dialog,null);  
            TextView text = (TextView) layout.findViewById(R.id.text);  
            text.setText("HI");  
            ImageView image = (ImageView) layout.findViewById(R.id.image);  
            image.setImageResource(R.drawable.icon);  
            builder = new AlertDialog.Builder(mContext);  
            builder.setView(layout);  
            alertDialog = builder.create();  
            alertDialog.show();  
        }  
    }  


5、最后执行之,点击Button
1
0
分享到:
评论

相关推荐

    Android开发实现自定义Toast、LayoutInflater使用其他布局示例

    "Android开发实现自定义Toast、LayoutInflater使用其他布局示例" Android开发实现自定义Toast、LayoutInflater使用其他布局是 Android 应用程序开发中非常重要的一部分。 Toast 是 Android 应用程序中最常用的提示...

    layoutinflater中嵌套layoutinflater

    标题提到的"layoutinflater中嵌套layoutinflater"涉及到的是在一个布局中使用`LayoutInflater`来加载另一个包含`LayoutInflater`的布局结构。这种操作通常出现在自定义复杂的可重用组件或者需要动态加载子视图的场景...

    Android 中级应用 一 LayoutInflater 的使用

    在Android应用程序中,我们通常使用XML来定义用户界面的布局,而`LayoutInflater`则起到了桥梁的作用,将静态的XML布局文件转换成可交互的UI组件。 `LayoutInflater`的基本用法包括以下步骤: 1. **获取实例**:...

    Android 中LayoutInflater的使用

    首先,我们来看一下如何使用LayoutInflater。在给定的例子中,我们有两个XML布局文件:`main.xml`和`custom_dialog.xml`。`main.xml`是应用的主要布局,包含一个TextView和一个Button。当用户点击Button时,我们希望...

    LayoutInflater源码分析 inflate方法的区别

    在`android_listview_inflater_demo`项目中,可能会展示如何使用`LayoutInflater`来填充ListView的每个条目。ListView通常使用Adapter,其中`getView`方法会调用`LayoutInflater`来创建和复用视图。通过合理使用`...

    LayoutInflater inflate 示例demo

    总的来说,这个"LayoutInflater inflate 示例demo"是一个很好的学习资源,它将帮助你深入理解Android中布局动态加载的过程,以及如何根据需要有效地使用`LayoutInflater`。通过实践,你将能够熟练掌握这一关键的...

    Android LayoutInflater的用法

    用于创建一个新的`LayoutInflater`实例,该实例具有与原始实例相同的布局工厂和标签前缀,但其上下文被替换为指定的新上下文,这对于处理多线程或在不同上下文中使用`LayoutInflater`非常有用。 源码分析方面,`...

    android中LayoutInflater的使用.pdf

    `LayoutInflater` 提供了一种灵活的方式来动态地加载和使用界面元素,使得开发者能够更方便地构建和定制用户界面。 `LayoutInflater` 的主要作用在于它可以从`res/layout`目录下的XML布局文件中创建视图层次结构。...

    Android LayoutInflater.Factory主题切换

    `LayoutInflater.Factory`的使用使得我们能够在创建View时根据当前主题应用相应的样式,提供了一种灵活且高效的方式来实现主题切换。在实际项目中,你可能还需要考虑缓存已创建的View,避免不必要的性能开销,以及...

    基于Android LayoutInflater的使用介绍

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

    LayoutInflater.from(context).inflate()方法的调研

    本文将深入探讨`LayoutInflater.from(context).inflate()`的工作原理、使用方法以及一些最佳实践。 首先,`LayoutInflater`是Android提供的一个类,它的主要任务是将XML布局文件解析成对应的View或ViewGroup对象。`...

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

    在Android应用开发中,我们通常使用LayoutInflater来动态地加载和插入布局,这在创建自定义视图、处理动态数据或者在运行时创建视图时非常有用。本文将深入解析LayoutInflater的工作原理,并提供实例代码来帮助理解...

    Android LayoutInflater深入分析及应用

    arser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Do nothing } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!...

Global site tag (gtag.js) - Google Analytics