将布局文件/res/layout/my_view.xml实例化为View对象,inflate()方法返回布局文件的view对象。
LayoutInflater.from(getContext()).inflate(int resource,ViewGroup root);//root为null,不把view添加到root中;root非空则添加。
LayoutInflater.from(getContext()).inflate(int resource,ViewGroup root,boolean attachToRoot);//root非空,attachToRoot为true,才把view添加到root中。
区别
因为View.inflate(context,layoutResId,root) 比 LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) 少了一个attachToRoot参数(是否将layoutResId添加到某个View中,作为其子View)。
在使用View.inflate(context,layoutResId,root) 时,如果root(父View)是null,会导致layoutResId布局中声明的宽高 + 外边距参数,失效。
核心条件就是root(父View)是不是null。
1、使用View.inflate(context,layoutResId,root) root不为null
2、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) root不为null,且attachToRoot是true
结果:两种方式效果相同,宽高 + 外边距 都有效
3、使用View.inflate(context,layoutResId,root) root为 null
4、使用LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot) root为 null,且attachToRoot是false
两种方式效果相同,宽高 + 外边距 都失效了,宽/高 变成wrap_content,一点要记住这点!!!是变成wrap_content。
至于为什么layoutResId布局宽度和父View一样,当子View失去自身LayoutParams(布局参数)后,父View会自动调整子View的宽高属性,下面会讲,先忽略。
5、如果不想将layoutResId布局添加到父View中,同时又不想丢失layoutResId布局中声明的参数,
LayoutInflater.from(context).inflate(layoutResId, root, attachToRoot)这样写可以做到,root不为null,但是attachToRoot为false。
6、而View.inflate(context,layoutResId,root) 目前为止无法做到,因为它少了一个attachToRoot参数(是否将layoutResId添加到某个View中,作为其子View)。
父View自动调整子View的宽高
当子View 没有 或 失去 自身LayoutParams(布局参数)后,父View会自动调整子View的宽高。
布局类型不同,子View宽高值也不同,说几个常用布局:
FrameLayout:宽 / 高 都会变成match_parent
RelativeLayout 和 ConstraintLayout 一样,宽 / 高 都会变成wrap_content
LinearLayout 设置vertical(垂直方向):宽变成match_parent,高变成wrap_content
LinearLayout 设置horizontal(水平方向):宽 / 高 都会变成wrap_content
layoutResId布局作为子View时,返回的是父布局View,反之返回的是layoutResId布局View。
来源:https://blog.csdn.net/Lan_Se_Tian_Ma/article/details/134819765
https://www.cnblogs.com/baipengzhan/p/LayoutInflater_inflate_View_inflate.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2020-12-25 winform程序backgroundworker或者异步中修改控件内容需要判断控件的InvokeRequired