LayoutInflater和inflate的用法,有图有真相

本文详细介绍了如何在Android中使用LayoutInflater和inflate方法动态加载布局文件,并通过示例展示了如何在Activity中加载和操作另一个布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity">  
  8.   
  9.   
  10.     <Button  
  11.         android:id="@+id/btn"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentBottom="true"  
  15.         android:layout_centerHorizontal="true"  
  16.         android:layout_gravity="center_horizontal"  
  17.         android:text="加载Titlebar" />  
  18.   
  19. </RelativeLayout>  

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

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/Titlebar"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="@color/lightblue"  
  7.   
  8.   
  9.     >  
  10.     <!--使用自定义圆形控件-->  
  11.     <com.elvis.layoutinflatedemo.CircleImageView  
  12.         android:id="@+id/pic"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_gravity="center_vertical"  
  16.         android:layout_marginLeft="10dp"  
  17.         android:layout_marginRight="4dp"  
  18.         android:src="@drawable/ic_launcher" />  
  19.     <!--分割线效果-->  
  20.     <ImageView  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_gravity="center_vertical"  
  24.         android:layout_marginLeft="6dp"  
  25.         android:layout_marginRight="6dp"  
  26.         android:src="@drawable/bar_divider" />  
  27.     <!--Title文本-->  
  28.     <TextView  
  29.         android:id="@+id/mytitle"  
  30.         android:layout_width="0dp"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_gravity="center_vertical"  
  33.         android:layout_marginLeft="6dp"  
  34.         android:layout_weight="1"  
  35.         android:text="模拟显示Title"  
  36.         android:textSize="20sp"  
  37.         android:textStyle="bold" />  
  38.   
  39.   
  40. </LinearLayout>  

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

[java]  view plain  copy
  1. public class MainActivity extends AppCompatActivity {  
  2.     private LayoutInflater mLayoutInflater;  
  3.     private RelativeLayout mainLayout;  
  4.     private Button mBtn;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         initViews();  
  11.         initEvents();  
  12.   
  13.     }  
  14.   
  15.     private void initEvents() {  
  16.         mBtn.setOnClickListener(new View.OnClickListener() {  
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 //点击动态加载布局  
  20.                 LinearLayout mlayout = (LinearLayout) mLayoutInflater.inflate(R.layout.titlebar, mainLayout, false);  
  21.                 //LinearLayout mlayout = (LinearLayout) mLayoutInflater.inflate(R.layout.titlebar, null);  
  22.                 //获取对应titleBar下的CircleImageView控件  
  23.                 CircleImageView myPic = (CircleImageView) mlayout.findViewById(R.id.pic);  
  24.                 CircleImageView myPic1 = (CircleImageView) findViewById(R.id.pic);  
  25.                 myPic.setImageResource(R.drawable.pic);  
  26.                 //获取对应titlebar下的TextView控件  
  27.                 TextView tx = (TextView) mlayout.findViewById(R.id.mytitle);  
  28.                 tx.setText("xsfelvis 优快云 博客");  
  29.   
  30.                 mainLayout.addView(mlayout);  
  31.             }  
  32.         });  
  33.     }  
  34.   
  35.     private void initViews() {  
  36.         mBtn = (Button) findViewById(R.id.btn);  
  37.         mainLayout = (RelativeLayout) findViewById(R.id.main);  
  38.         mLayoutInflater = LayoutInflater.from(this);  
  39.   
  40.     }  
  41.   
  42. }  

如果你把

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


转载:http://blog.youkuaiyun.com/xsf50717/article/details/49446687

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值