`

LayoutInflater作用及使用

 
阅读更多

作用: 
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 

2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素. 

方法: 
   Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面. 

   其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml, 

 
下面是一个简单的例子:
 
首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
 
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
为了让大家容易理解我[转]做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
LayoutInflater作用及使用

代码如下:
package com.bivin;

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 MainActivity 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 = MainActivity.this;

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("Hello, Welcome to Mr Wei's blog!");
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();
}
}
 
分享到:
评论

相关推荐

    Android 中级应用 一 LayoutInflater 的使用

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

    Android 中LayoutInflater的使用

    1. **LayoutInflater的作用**:将XML布局文件转换为视图对象,使得我们可以在运行时动态地创建和操作界面。 2. **使用方法**:通过`LayoutInflater.from(Context)`获取LayoutInflater实例,然后调用`inflate()`方法...

    LayoutInflater的使用

    它的作用类似于 findViewById(),不同点是 LayoutInflater 是用来找 layout 文件夹下的 xml 布局文件,并且实例化!而 findViewById() 是找具体某一个 xml 下的具体 widget 控件(如:Button、TextView 等)。 ...

    android中LayoutInflater的使用.pdf

    在本文中,我们将深入探讨`LayoutInflater`的使用及其重要性。 `LayoutInflater`的主要功能: 1. **实例化布局**: 与`findViewById()`不同,`LayoutInflater`是用来加载整个XML布局文件,而不是单个视图控件。通过`...

    Android开发中LayoutInflater用法详解

    在本文中,我们将深入探讨LayoutInflater的工作原理、使用方法以及如何获取其实例。 首先,LayoutInflater的作用在于将XML布局资源解析并转换为Android视图对象(View或ViewGroup)。与`findViewById()`不同,`...

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

    在本文中,我们将深入探讨使用LayoutInflater时需要注意的一些关键点,以避免潜在的问题和陷阱。 首先,理解LayoutInflater.inflate()方法的三个参数至关重要。这个方法通常用于动态加载XML布局并将其插入到视图...

    AsyncTask、JSONAdapter、LayoutInflater的实例

    例如,当你需要动态添加或者删除布局时,如在聊天应用中添加新的消息项,或者在设置界面中根据用户选择添加或移除选项,LayoutInflater就起到了核心作用。 综合运用这三个概念,可以实现高效且流畅的Android应用。...

    基于Android LayoutInflater的使用介绍

    `LayoutInflater`的作用就是将这些静态的XML布局转化为可以在屏幕上显示的动态视图对象。与`Activity`中的`findViewById()`方法不同,`LayoutInflater`不寻找具体的控件,而是解析整个布局文件并创建其中包含的所有...

    Android LayoutInflater中 Inflate()方法应用

    `inflate()` 方法是`LayoutInflater` 的核心方法,它的作用是解析XML布局文件,并将其转换为Android视图层次结构。下面将详细阐述`inflate()` 方法的用法及其与其他方法的区别。 首先,`inflate()` 方法的基本使用...

    关于Android开发的一些笔记

    1. **`LayoutInflater`的作用**: - `LayoutInflater`的主要功能是将XML布局文件转换为Android运行时可以操作的视图对象。这使得开发者可以在程序运行时动态地加载和插入布局,而不是在应用程序启动时一次性加载...

    使用inflater实现窗体布局

    这篇博客“使用inflater实现窗体布局”将深入探讨如何使用`LayoutInflater`来实现这一功能。 `LayoutInflater`是Android SDK中的一个关键类,它位于`android.view.LayoutInflater`包中。它的主要作用是从XML布局...

    自定义Adapter并通过布局泵LayoutInflater抓取layout模板编辑每一个item实现思路

    LayoutInflater是Android中的一个类,它的主要作用是从XML布局文件中加载并解析布局,将其转化为View对象。在自定义Adapter时,我们通常会用到LayoutInflater的inflate()方法来创建和填充每个item的视图。 在自定义...

    LayouInflater

    `LayoutInflater` 的作用在于动态地创建UI元素,使得开发者可以灵活地构建和更新界面。在"android LayouInflater 页面跳转 button点击"这个主题中,我们将深入探讨`LayoutInflater`的使用以及与页面跳转和按钮点击...

    新闻界面的实现-碎片的使用

    - 创建一个新的Java类,继承自Fragment类,并重写必要的方法,如onCreateView(),在此方法中使用LayoutInflater来创建布局。 - 在XML布局文件中定义碎片的UI结构,通常包含一个根布局,如LinearLayout、...

    Android inflater 用法及不同点

    在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件...

    深入解析Android App的LayoutInflate布局

    在这个例子中,我们使用`LayoutInflater`的`inflate()`方法加载`item_button`布局,并将其添加到`ListView`的`parent`中,但不直接附加(`false`)。尽管如此,`Button`的宽高设置依然有效,因为它在解析时使用了`...

Global site tag (gtag.js) - Google Analytics