`
252190908
  • 浏览: 229043 次
文章分类
社区版块
存档分类
最新评论

LayoutInflater作用及使用

 
阅读更多
作用:<wbr><br> 1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.<wbr><br><br> 2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.<wbr><br><br> 方法:<wbr><br><wbr><wbr>Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面.<wbr><br><br><wbr><wbr>其实在Activity里面就使用了LayoutInflater来载入界面, 通过<span style="color:#6E29FE; word-wrap:normal; word-break:normal"><strong>getSystemService(Context.LAYOUT_INFLATER_SERVICE)</strong></span>方法可以获得一个 LayoutInflater, 也可以通过<span style="color:#3516F8; word-wrap:normal; word-break:normal"><strong>LayoutInflater inflater = getLayoutInflater();</strong></span>来获得.然后使用inflate方法来载入layout的xml,<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

下面是一个简单的例子:

首先我们要知道,什么是已经被载入的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();
}
}
分享到:
评论

相关推荐

    LayoutInflater的使用

    LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!

    Android开发中LayoutInflater用法详解

    主要介绍了Android开发中LayoutInflater用法,结合实例形式分析了LayoutInflater类的功能、作用、使用方法及相关注意事项,需要的朋友可以参考下

    Android LayoutInflater中 Inflate()方法应用

    Android Inflate()方法的作用是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。 Android上还有一个与Inflate()功能类似的方法叫...

    Android inflater 用法及不同点

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

    我在学习Andrid Studio时候碰到的小问题

    1.注意!实例化过的控件,例如Textview tx,如果再次...首先,获取需要加载的布局文件order.xml, 这里采用的是LayoutInflater,而不是我们平时使用的 findViewById( ). **LayoutInflater的作用类似于 findViewById

    xmljava系统源码-Skin:网易云音乐换肤方案

    在详细实现之前,先介绍LayoutInflater,因为它在换肤中非常重要:它的作用是:将布局XML文件实例化为其对应的View对象。所以可以通过LayoutInflater获取到创建了多少View以及View对应的属性。 这里有鸿洋大佬的一篇...

    RecyclerViewPager-一个基于RecyclerView实现的ViewPager,支持类似于gallary的fling操作.zip

    运行效果:使用说明:继承自 RecyclerView.自定义 fling factor.自定义 paging trigger.支持 水平个垂直方向.支持 FragmentViewPager (api 12 )gradlecompile('com.lsjwzh:recyclerviewpager:1.0.2')xml:  ...

    autoJS1688示例.zip

    00-本地时间及网络时间验证改版.js 00-正则匹配关闭应用-适用大部分手机(1).js 00-正则匹配关闭应用-适用大部分手机.js 00-简化点击控件.js 00-结束事件与结束应用(1).js 00-结束事件与结束应用.js 00-读&删...

Global site tag (gtag.js) - Google Analytics