1、概述

有时候在我们的Activity中用到别的layout,并且要对其组件进行操作,比如:

A.acyivity是获取网络数据的,对应布局文件为A.xml,然后需要把这个数据设置到B.xml的组件上,咋办?这时候你就需要使用inflate()方法了

2、LayoutInflater和inflate的用法

2.1、LayoutInflater

【LayoutInflater】其实是在res/layout/下找到xml布局文件,并且将其实例化,对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

【findViewById】是找出xml布局文件下的具体widget控件(如Button、TextView等)通常是对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。



在获取布局之前首先要对LayoutInflater进行实例化,通常有以下三种方式

【1】 LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()

【2】LayoutInflater inflater = LayoutInflater.from(context);  

【3】 LayoutInflater inflater =  (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

其实这三种方式的本质都是相同的,getLayoutInflater()——>调用LayoutInflater.from(context)——>调用context.getSystemService(),最终还是调用底层service服务

2.2、inflate

inflate就相当于将一个xml中定义的布局找出来,常用方法

【1】inflate(int resource,null)

【2】inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义

参数一  resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。



参数二  root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。



参数三  attachToRoot:是否将root附加到布局文件的根视图上,要是设置为true的话必须是前面俩个布局类型一致,比如同为线程布局或者同为相对布局。否则会报错

3、实例

通过button加载另外一个A布局文件到主布局上,并且通过inlfate对A布局控件做了设置,下图所示:

主布局文件,注意这里是相对布局,很简单一个button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:text="加载Titlebar" />

</RelativeLayout>

titleBar布局,使用了一个自定义圆形图片控件,加了一些效果,这里图片和文字内容都是默认,并不是上图显示的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Titlebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/lightblue"

    >
    <!--使用自定义圆形控件-->
    <com.elvis.layoutinflatedemo.CircleImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="4dp"
        android:src="@drawable/ic_launcher" />
    <!--分割线效果-->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:src="@drawable/bar_divider" />
    <!--Title文本-->
    <TextView
        android:id="@+id/mytitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="6dp"
        android:layout_weight="1"
        android:text="模拟显示Title"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

MainActivty,动态设置了图片和标题内容并将其添加到主布局中

public class MainActivity extends AppCompatActivity {
    private LayoutInflater mLayoutInflater;
    private RelativeLayout mainLayout;
    private Button mBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();

    }

    private void initEvents() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击动态加载布局
                LinearLayout mlayout = (LinearLayout) mLayoutInflater.inflate(R.layout.titlebar, mainLayout, false);
                //LinearLayout mlayout = (LinearLayout) mLayoutInflater.inflate(R.layout.titlebar, null);
                //获取对应titleBar下的CircleImageView控件
                CircleImageView myPic = (CircleImageView) mlayout.findViewById(R.id.pic);
                CircleImageView myPic1 = (CircleImageView) findViewById(R.id.pic);
                myPic.setImageResource(R.drawable.pic);
                //获取对应titlebar下的TextView控件
                TextView tx = (TextView) mlayout.findViewById(R.id.mytitle);
                tx.setText("xsfelvis CSDN 博客");

                mainLayout.addView(mlayout);
            }
        });
    }

    private void initViews() {
        mBtn = (Button) findViewById(R.id.btn);
        mainLayout = (RelativeLayout) findViewById(R.id.main);
        mLayoutInflater = LayoutInflater.from(this);

    }

}

如果你把

LinearLayout mlayout = (LinearLayout) mLayoutInflater.inflate(R.layout.titlebar, mainLayout, false);中fasle改为true就会报错,这也印证了开篇所说的内容,使用的时候留点心吧!

LayoutInflater和inflate的用法,有图有真相的更多相关文章

  1. 转载 LayoutInflater的inflate函数用法详解

    http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...

  2. 转载《 LayoutInflater 的inflate函数用法详解》

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  3. LayoutInflater的inflate函数用法详解

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  4. LayoutInflater的inflate函数用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  5. [转]LayoutInflater的inflate函数用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  6. Android成长之路-LayoutInflater和inflate的用法

    在这里用Tabhost的例子来说明: package cn.csdn.activity; import android.app.TabActivity; import android.os.Bundl ...

  7. getViewById和getLayoutInflater().inflate的用法

    getViewById和getLayoutInflater().inflate得用法 1.什么是LayoutInflaterThis class is used to instantiate layo ...

  8. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  9. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

随机推荐

  1. Ubuntu批量修改文件名后缀

    比如把当前文件夹下所有scss文件后缀改为less rename 's/\.scss/\.less/' ./*

  2. Twitter 架构优化之路--Twitter是如何做到每秒处理3000张图片的

    如今,Twitter每秒可以创建并保存3000张(20GB)的图片.2015年,Twitter甚至从对媒体存储策略的优化中节省出了600万美元. 但并非一开始就是这样的,2012年Twitter还主要 ...

  3. ELK搭建

    ELK安装 elasticsearch安装 * 下载elasticsearch-5.0.0.tar.gz,并解压. 通过elasticsearch.yml可设置host和port. vim confi ...

  4. 23 服务的创建Demo1

    结构 MainActivity.java package com.qf.day23_service_demo1; import android.app.Activity; import android ...

  5. 使用spark ml pipeline进行机器学习

    一.关于spark ml pipeline与机器学习 一个典型的机器学习构建包含若干个过程 1.源数据ETL 2.数据预处理 3.特征选取 4.模型训练与验证 以上四个步骤可以抽象为一个包括多个步骤的 ...

  6. Android fragment(片段)构建灵活的UI

    在以支持多种屏幕尺寸为目标设计应用时,您可以在不同的布局配置中重复使用您的fragment 从而根据可用的屏幕空间优化用户体验. 例如,在手机设备上,由于采用单窗格用户界面,因此可能更适合一次只显示一 ...

  7. [Pelican]Pelican入门(二)

    之前是搭建了一个简单的博客,但是没有图片,没有具体的栏目分类 这次来研究下 一 导航栏 之前是直接把.md扔到的content文件夹下,结果导航栏,显示的是Category信息. 现在这么改成 D:. ...

  8. JAVA面向对象-----main方法详解

    JVM看不懂的可以跳过,这里不做过多解释,(^__^) 嘻嘻-- 主函数是静态的 public static void main(String[] args){ } 主函数是什么:主函数是一个特殊的函 ...

  9. 13 SQLiteOpenHelper SQLiteDatabase详解

    创建数据库: 1. 创建一个类继承SQLiteOpenHelper 2. 创建继承对象 new SQLiteOpenHelper() 3. 用创建的对象获取可写或者可读的SQLiteDatabase ...

  10. Android简易实战教程--第十三话《短信备份和还原~三》

    之前写过短信备份的小案例,哪里仅仅是虚拟了几条短信信息.本篇封装一个业务类,且直接通过内容提供者,访问本系统的短信信息,再提供对外接口.如果想要短信备份和短信还原,直接复制这段代码即可.对于您调用这个 ...