LayoutInflater和inflate的用法

一、概述

有时候在咱们的Activity中用到别的layout,而且要对其组件进行操做,好比:php

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

二、LayoutInflater和inflate的用法

2.一、LayoutInflater

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

【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);ide

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

2.二、inflate

inflate就至关于将一个xml中定义的布局找出来,经常使用方法this

【1】inflate(int resource,null)spa

【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的话必须是前面俩个布局类型一致,好比同为线程布局或者同为相对布局。不然会报错code

 

三、实例

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

height=738

你的生肖是穷苦命,仍是富贵命!

【点击进入】

你的生肖决定你是穷苦命,仍是富贵命, 12生肖本命佛【镇宅化煞】招财转运

查 看

height=737

你的生肖是穷苦命,仍是富贵命!

【点击进入】

你的生肖决定你是穷苦命,仍是富贵命, 12生肖本命佛【镇宅化煞】招财转运

查 看

 

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

 

?

1

<relativelayout android:id="@+id/main" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"><button android:id="@+id/btn" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="加载Titlebar"></button></relativelayout>


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

 

 

?

1

2

3

4

5

6

7

8

9

10

11

<!--?xml version=1.0 encoding=utf-8?-->

<linearlayout android:background="@color/lightblue" android:id="@+id/Titlebar" android:layout_height="wrap_content" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">

    <!--使用自定义圆形控件-->

    <com.elvis.layoutinflatedemo.circleimageview android:id="@+id/pic" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:layout_marginright="4dp" android:layout_width="wrap_content" android:src="@drawable/ic_launcher">

    <!--分割线效果-->

    <imageview android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:layout_marginleft="6dp" android:layout_marginright="6dp" android:layout_width="wrap_content" android:src="@drawable/bar_divider">

    <!--Title文本-->

    <textview android:id="@+id/mytitle" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:layout_marginleft="6dp" android:layout_weight="1" android:layout_width="0dp" android:text="模拟显示Title" android:textsize="20sp" android:textstyle="bold">

 

 

</textview></imageview></com.elvis.layoutinflatedemo.circleimageview></linearlayout>


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

 

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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就会报错,这也印证了开篇所说的内容,使用的时候留点心吧!

height=89

相关文章
相关标签/搜索