Android之LayoutInflater加载布局及原理分析

相信以前写过Android的朋友都会经常遇到这么一个东西:LayoutInflater,可能许多初学者都是直接在Activity里绘制或者控制那个Activity里的内容(直接setContentView(布局文件)),但是当你需要在一个Activity里加载或者控制另外一个Activity的控件或者动态加载View时,你就会需要用到它。


LayoutInflater的基本用法

主要分为两步:1.获得LayoutInflater实例   2.通过LayoutInflater实例调用inflate()方法获得相应的view

第一步,获得LayoutInflater实例有两种方式可以实现:
1.LayoutInflater inflater = LayoutInflater.from(context);
2.LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

第二步:

View view = inflater.inflate(resourceId, root);

inflate方法中传入两个参数,第一个表示你所要加载的资源文件的ID,第二个表示为它加上一层父布局(不需要的时候可以传null).


下面通过一个简单的例子,在当前布局中加载另外一个布局,并将其添加到当前布局中:

主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mylinearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

</LinearLayout>


button布局文件【mybutton.xml】:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 	
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是被inflater加载的按钮"
        />
    
</LinearLayout>


Activity代码:

public class SecondActivity extends Activity {
	
	private LinearLayout mylinearlayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		//获得当前Activity的layout实例
		mylinearlayout = (LinearLayout)this.findViewById(R.id.mylinearlayout);
		
		//通过当前context获得LayoutInflater实例
		LayoutInflater inflater = LayoutInflater.from(this);
		
		//调用inflate加载mybutton.xml布局,返回的是按钮布局的view实例
		View buttonview = inflater.inflate(R.layout.mybutton, null);
		
		//将得到的view实例添加到当前Activity的layout里面去
		mylinearlayout.addView(buttonview);
	}
}


运行结果如下,可以看到button布局已经成功添加到主布局中:




LayoutInflater与setContentView

在上面的例子中,如果改变button的大小为具体数值,比如设置为130*150:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 	
        android:layout_width="130dp"
        android:layout_height="150dp"
        android:text="这是被inflater加载的按钮"
        />
    

</LinearLayout>


结果:




可以看到button随着我们的改变而改变,那如果我们将button.xml里按钮的父布局去掉之后,会是怎样的效果呢?

button.xml改为如下:

<?xml version="1.0" encoding="utf-8"?>  
<Button xmlns:android="http://schemas.android.com/apk/res/android"	
     android:layout_width="130dp"
     android:layout_height="150dp"
     android:text="这是被inflater加载的按钮"
     />



再次运行:




你会惊奇地发现,按钮怎么变回原来的那个大小了,明明我是设置了具体大小的呀,为何失效了?
其实这里不管你将Button的layout_width和layout_height的值修改成多少,都不会有任何效果的,因为这两个值现在已经完全失去了作用。上面的例子中我们设置了具体大小却有效果,去掉了父布局之后就没了效果,这是因为只有在有父布局的情况下,对button设置大小才会有效果,其实layout_width和layout_height是表示控件在父布局中的大小的,所以没有了父布局,也就谈不上改变它的大小了,这也是为什么这两个属性叫作layout_width和layout_height,而不是width和height。


这时,你又会有了疑问,那我平常直接使用Activity的布局文件时,它的最外层的那个布局是可以调整大小的呀?
其实我们平常setContentView的时候,系统就会为我们的布局外面再套上一层FrameLayout,所以最外层的那个layout就依然可以调整layout_width和layout_height。

我们可以通过获取它的父布局来进行验证:

public class SecondActivity extends Activity {
	
	private LinearLayout mylinearlayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		//获得当前Activity的layout实例
		mylinearlayout = (LinearLayout)this.findViewById(R.id.mylinearlayout);
		
		ViewParent parent = mylinearlayout.getParent();
		Log.d("LayoutInflater---->", ""+parent);
		
		
	}
}


Logcat打印结果:


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值