Comments (6)
你好,我想咨询一下,如果不添加这个 parent 这个参数,在什么场景中,会出现什么问题,因为我使用 Navigation 的场景不是很多
from binding.
你好,我想咨询一下,如果不添加这个 parent 这个参数,在什么场景中,会出现什么问题,因为我使用 Navigation 的场景不是很多
parent传null会使根部局的layout属性失效。
from binding.
你好,我想咨询一下,如果不添加这个 parent 这个参数,在什么场景中,会出现什么问题,因为我使用 Navigation 的场景不是很多
parent传null会使根部局的layout属性失效。
我先说一下我的理解。
layout属性我理解为是 layout_width 、layout_height
根部局: 我的理解是 layout 布局文件的根 view ,即 rootView
根据源码我的理解是这样的:
parent 传 null 不会使用 parent 的布局参数,但是会使用根部局的 layout 属性, 也就是说 layout 布局文件的根 view 布局参数是生效的 ,而一般我们很少会使用 parent 的布局参数
源码如下所示:
public static FragmentNav1Binding inflate(@NonNull LayoutInflater inflater,
@Nullable ViewGroup parent, boolean attachToParent) {
View root = inflater.inflate(R.layout.fragment_nav_1, parent, false);
if (attachToParent) {
parent.addView(root);
}
return bind(root);
}
attachToParent 为 true 会将 inflater.inflate 返回的 view 添加进 parent 中
inflater.inflate 源码大概意思如下所示:
- 当 attachToRoot == true 且 root != null 时,新解析出来的 View 会被 add 到 root 中去,然后将 root 作为结果返回
- 当 attachToRoot == false 且 root != null 时,新解析的 View 会直接作为结果返回,而且 root 会为新解析的 View 生成 LayoutParams 并设置到该 View 中去
- 当 attachToRoot == false 且 root == null 时,新解析的 View 会直接作为结果返回
from binding.
我在实践中是生效的,能举一个不生效的例子吗,我研究一下,做一下特殊情况下的适配,非常感谢
from binding.
root为null的话,被inflate的layout的根布局的layout_width、layout_height不生效,会导致某些布局宽高不符合预期。
我随便找的两篇文章,你可以参考验证一下:
1、http://hoyouly.fun/2020/04/07/inflate/ (扫盲系列 - layoutInflater中 inflate() 参数总结)
2、https://blog.csdn.net/u012702547/article/details/52628453 (三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别)
from binding.
非常感谢你的提交,这个问题在 1.0.9 上修复了,之前旧的 方式一 依然保留,但是不建议在使用了
from binding.
Related Issues (20)
- 在Navigation启动的Fragment中使用报错 HOT 1
- ActivityDataBinding里OS版本小于Q时为什么要添加一个空白的fragment? HOT 1
- 在BRAVH中怎么使用呢 HOT 1
- NoSuchMethodException问题 HOT 2
- 使用viewbind时,如何在使用xml布局的自定义ViewGroup中使用呢 HOT 4
- 版本时间写错啦,都2021年啦 HOT 5
- ActivityViewBinding 是否应该主动在onCreate时调用setContentView HOT 1
- 自定义View的混淆规则 HOT 2
- 遇到个bug HOT 9
- Fatal Exception: java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager. HOT 13
- 更新kotlin后,出现一下问题 HOT 2
- 关于ViewGroupDataBinding的一个问题 HOT 6
- SmartRefreshLayout自定义RefreshHeader时,报java.lang.NoSuchMethodException的错误 HOT 13
- 1.1.6版本找不到databind了 HOT 3
- 同一类型的多个 fragment 实例的 binding.view 引用错位问题 HOT 2
- Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option HOT 1
- onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager HOT 4
- 开启混淆会报错,按照文档添加了混淆规则
- 有一定的几率会报错闪退,Android 12 红米手机 HOT 1
- -
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from binding.