LayoutInflater.inflate()的参数详解

LayoutInflater.inflate()

inflate() v. 充气,打气

inflate就是将一个xml布局变成View,

注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。

1. 获取LayoutInflater对象有三种方法

1. LayoutInflater inflater=LayoutInflater.from(this);
2. LayoutInflater inflater=getLayoutInflater();
3. LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

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

resource:需要加载的布局文件的id

root:就是这个resourse要插入的布局

attachToRoot:是否将这个生成的View添加到这个root中去

总结:
首先看带三个参数的inflate方法:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1、如果root不为null,且attachToRoot为TRUE,则会在加载的布局文件的最外层再嵌套一层root布局(root.addView(temp, params)),
这时候xml根元素的布局参数当然会起作用。

2、如果root不为null,且attachToRoot为false,则不会在加载的布局文件的最外层再嵌套一层root布局,
这个root只会用于为要加载的xml的根view生成布局参数(temp.setLayoutParams(params)),这时候xml根元素的布局参数也会起作用了!!!

3、如果root为null,则attachToRoot无论为true还是false都没意义!即xml根元素的布局参数依然不会起作用!

示例:
LayoutInflater的使用中重点关注inflate方法的参数含义:

inflate(xmlId, null); 只创建temp的View,然后直接返回temp。

inflate(xmlId, parent); 创建temp的View,然后执行root.addView(temp, params);最后返回root。

inflate(xmlId, parent, false); 创建temp的View,然后执行temp.setLayoutParams(params);然后再返回temp。

inflate(xmlId, parent, true); 创建temp的View,然后执行root.addView(temp, params);最后返回root。

inflate(xmlId, null, false); 只创建temp的View,然后直接返回temp。

inflate(xmlId, null, true); 只创建temp的View,然后直接返回temp。

例子

  1. 用listview来验证上面的理论

    问题: 在ListView的item布局中设置的高度没有效果的问题。
    
    item_lv_test.xml :
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="100dip"  *****
        android:gravity="center_vertical"  
        android:orientation="horizontal">  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="test" />  
    </LinearLayout>
    
    adapter的getView方法 : 
    public View getView(int position, View convertView, ViewGroup parent) {  
        if (convertView == null) {  
            convertView = inflate(R.layout.item_lv_test, null);  //设置100dp是无效的
    
            //这样就可以了
            convertView = inflate(R.layout.item_lv_test, parent, false);  
        }  
        return convertView;  
    } 
    
  2. 用LinearLayout验证上面的问题

    要添加的View的布局
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="80dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@android:color/holo_green_dark"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/iv_media_menu_icon"
            android:layout_height="24dp"
            android:layout_width="24dp"
            android:src="@drawable/ic_mv"
            android:layout_centerHorizontal="true"/>
        <TextView
            android:id="@+id/tv_media_menu_text"
            android:text="bxbxbai"
            style="@style/Menu_TextView"/>
    </RelativeLayout>
    
    Java代码中: 
    LinearLayout layout = (LinearLayout)findViewById(R.id.container);
    View view = View.inflate(this, R.layout.layout_menu_item, null);
    layout.addView(view);
    这样写的话,你就会发现布局文件R.layout.layout_menu_item中的android:layout_width="80dp"不起作用!!也就是说View.inflate方法忽略了布局文件的宽度设置
    
    下面改写代码:
    LinearLayout layout = (LinearLayout)findViewById(R.id.container);
    View view = View.inflate(this, R.layout.layout_menu_item, layout);
    layout.addView(view);
    
    你就会发现这样写会崩溃!然后下面这样写就没问题了:
    LinearLayout layout = (LinearLayout)findViewById(R.id.container);
    View view = View.inflate(this, R.layout.layout_menu_item, layout);
    
    View.inflate方法自动将生成的View添加到了这个ViewGroup root中去了!!
    
    解释:
    * View.inflate(Context context, int resource, ViewGroup root),
    * 这个方法本质上是调用了LayoutInflater.from(context).inflate(resource, root, root != null),在这个inflate方法中可以找到下面代码:
    
    if (root != null && attachToRoot) {
        root.addView(temp, params);
    }
    可见inflate方法自动将这个生成的View添加到了这个root中去了
    

一般规律
如果要加载的资源xml文件设置了参数(layout_*),则需要设置root,如果没有,则不需要。

如果资源布局是这样的话,需要设置parent,否则布局中的layout_*参数不起作用.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Text Test"
android:background="#ffa0a00c"/>

- 我们经常使用View的layout_ width和layout_ height来设置View的大小,而且一般都可以正常工作,所以有人时常认为这两个属性就是设置View的真实大小一样;然而实际上这些属性是用于设置View在ViewGroup布局中的大小的;这就是为什么Google的工程师在变量命名上将这种属性叫作layout_ width和layout_ height,而不是width和height的原因了。

因为layout_ height是相对了父级设置的,而此temp的getLayoutParams为null。

-

如果资源布局是这样的话,什么都不用管

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Text Test"
        android:background="#ffa0a00c"/>

</LinearLayout>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值