android动态写布局,动态加载布局文件的3种方法

前言

LayoutInflater意思是:布局填充器,可以把布局填充成View对象,以下两篇文章可以对比着看:

1. 动态加载布局有3种方法

// 第一种

View layoutView = View.inflate(this , R.layout.activity_main , null) ;

// 第2种

layoutView = LayoutInflater.from(this).inflate(R.layout.activity_main , null) ;

// 第3种

layoutView = LayoutInflater.from(this).inflate(R.layout.activity_main , null , false) ;

分析:

1>:点击第一种的View.inflate()进入源码会发现,其实是调用的第2种方法:

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {

LayoutInflater factory = LayoutInflater.from(context);

return factory.inflate(resource, root);

}

2>:点击第二种的LayoutInflater.from(this).inflate进入源码会发现,其实是调用的第3种方法:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {

return inflate(resource, root, root != null);

}

所以我们只需要分析第3种方法就ok,那么接下来我们先来看下是如何获取LayoutInflater.from(this)的实例的,然后再来分析如何解析View的?

2. 分析第三种:LayoutInflater.from(this).inflate(R.layout.activity_main , null , false)

参数1:要加载的布局文件;

参数2:父布局容器;

参数3:false表示不把布局文件添加到容器中,true表示把布局文件添加到容器中;

注意:

如果第三个参数为false,但是如果添加这句代码,表示和true一样,都可以把布局添加到容器中:

frameLayout.addView(inflateView);

3. 常见开发场景 —— 开发一般用第三种方式

修改前

比如给RecyclerView添加头布局,下边中 inflate()方法中给 父布局容器传递的是 null,可以实现给 RecyclerView添加头布局,但是头部只能显示一小部分,原因就是 父布局容器传递的是null:

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view) ;

// RecyclerView必须设置布局管理,否则没有数据

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

View headView = LayoutInflater.from(this).inflate(R.layout.layout_header_view , null , false) ;

wrapRecyclerAdapter.addHeaderView(headView);

效果图如下

cdb6833e7fe8

图片.png

修改后

给 inflate()方法中的 父布局容器传递 mRecyclerView,就可以让头部宽度充满屏幕

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view) ;

// RecyclerView必须设置布局管理,否则没有数据

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

View headView = LayoutInflater.from(this).inflate(R.layout.layout_header_view , mRecyclerView , false) ;

wrapRecyclerAdapter.addHeaderView(headView);

效果图如下

cdb6833e7fe8

图片.png

4. 举例说明

1>:父布局容器activity_second.xml布局文件如下:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/checkBox1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="复选框" />

2>:需要添加到容器中的布局文件inflate_view_layout如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello World" />

android:id="@+id/fl_container"

android:layout_width="match_parent"

android:layout_height="wrap_content">

3>:测试的SecondActivity如下:

/**

* Email: 2185134304@qq.com

* Created by Novate 2018/5/4 16:03

* Version 1.0

* Params:

* Description: 动态添加布局到容器中

*/

public class SecondActivity extends AppCompatActivity {

private FrameLayout fl_container;

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

fl_container = (FrameLayout) findViewById(R.id.fl_container);

/**

* 参数1:需要动态添加的布局文件

* 参数2:父布局容器

* 参数3:false表示没有把布局文件添加到父布局中,true表示把布局文件添加到容器中

*/

// 第三个参数设置为false

View inflateView = LayoutInflater.from(SecondActivity.this).inflate(R.layout.inflate_view_layout, fl_container, false);

/*// 第三个参数设置为true -> 可以把布局文件添加到父布局中

View inflateView = LayoutInflater.from(SecondActivity.this).inflate(R.layout.inflate_view_layout, fl_container, true);*/

/*// 第三个参数设置为false + addView -> 可以把布局文件添加到父布局中

View inflateView = LayoutInflater.from(SecondActivity.this).inflate(R.layout.inflate_view_layout, fl_container, false);

fl_container.addView(inflateView);*/

}

}

第三个参数设置为false效果如下,没有把布局文件添加到容器中:

cdb6833e7fe8

图片.png

第三个参数设置为true或者第三个参数设置为false + addView 效果如下,可以把布局文件添加到容器中:

cdb6833e7fe8

图片.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值