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

前言

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

Android中实例化布局文件有2种方式
Android中动态加载布局文件的3种方法

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);
  • 效果图如下


    图片.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);
  • 效果图如下


    图片.png

4. 举例说明


1>:父布局容器activity_second.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">

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="复选框" />

</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

</LinearLayout>

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效果如下,没有把布局文件添加到容器中:

图片.png

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

图片.png

代码已上传至github:
https://github.com/shuai999/DynamicViewDemo.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容