java layoutinflater_LayoutInflater实例获取和使用

Xml代码  a4c26d1e5885305701be709a3d33442f.png

<LinearLayout

android:id="@+id/ll_item_Group"

android:layout_width="match_parent"

android:layout_height="200dp"

android:background="#FF0000"

android:orientation="vertical">

LinearLayout>

Java代码  a4c26d1e5885305701be709a3d33442f.png

itemGroup = (LinearLayout) findViewById(R.id.ll_item_Group);

这个作为itemGroup对象。

Xml代码  a4c26d1e5885305701be709a3d33442f.png

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<LinearLayout

android:id="@+id/view_content"

android:layout_width="match_parent"

android:layout_height="100dp"

android:background="#4169E1"

android:orientation="horizontal">

LinearLayout>

<RelativeLayout

android:id="@+id/view_todo"

android:layout_width="100dp"

android:layout_height="match_parent"

android:background="#00008B">

RelativeLayout>

LinearLayout>

这个作为include引用的view。测试代码如下:(inflater是LayoutInflater对象的实例,获取方法是:inflater

= LayoutInflater.from(this),其它两种方法自己百度)

Java代码  a4c26d1e5885305701be709a3d33442f.png

View v1 = inflater.inflate(R.layout.el_include,null);

View v3 = inflater.inflate(R.layout.el_include, itemGroup, false);

View v2 = inflater.inflate(R.layout.el_include, itemGroup);

View v4 = inflater.inflate(R.layout.el_include, itemGroup, true);

测试结果是:

1、V1和V3在Activity里显示效果一样,都是itemGroup原来的内容,V1和V3都是R.layout.el_include里的View对象。

2、V2和V4在Activity里显示效果一样,都是itemGroup添加R.layout.el_include里的内容之后的。V2和V4对象都是加了R.layout.el_include的itemGroup。

V2和V4在Activity里显示效果一样说明itemGroup没有改变!

V2和V4在Activity里显示效果一样说明itemGroup发生了改变,都是将R.layout.el_include里的内容添加到了itemGroup之后的View

特别说明:V1和V3的区别

若我们采用convertView =

inflater.inflate(R.layout.item_list,

null);方式填充视图,item布局中的根视图的layout_XX属性会被忽略掉,然后设置成默认的包裹内容方式,也就是说布局文件中item的根视图所有设置的属性都起作用如果我们想保证item的视图中的参数不被改变,我们需要使用convertView

=

inflater.inflate(R.layout.item_list,

parent,false);这种方式进行视图的填充除了使用这种方式,我们还可以设置item布局的根视图为包裹内容,然后设置内部控件的高度等属性,这样就不会修改显示方式了

merge和include的区别是:

include所引用的就是一个独立的View,而merge引用的View必须放到一个ViewGroup中。如下例:

Xml代码  a4c26d1e5885305701be709a3d33442f.png

<mergexmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:id="@+id/view_content"

android:layout_width="match_parent"

android:layout_height="100dp"

android:background="#4169E1"

android:orientation="horizontal">

LinearLayout>

<RelativeLayout

android:id="@+id/view_todo"

android:layout_width="100dp"

android:layout_height="match_parent"

android:background="#800080">

RelativeLayout>

merge>

R.layout.el_marge

引用必须是这样的:

Java代码  a4c26d1e5885305701be709a3d33442f.png

View v = inflater.inflate(R.layout.el_marge, itemGroup,true);

否则报错: can be

used only with a valid ViewGroup root and

attachToRoot=true

也就是说:merge是为了减少include里的根ViewGroup,那么inflate的marge必须放到ViewGroup中。

网上也有老说到marge和framelayout,其实我觉得没有联系。就是R.layout.el_marge若不添加一个ViewGroup中的它里面的元素而已规则和FrameLayout一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值