What is the LayoutInflater in Android? How does it differs from findViewById?

huangapple go评论57阅读模式
英文:

What is the LayoutInflater in Android? How does it differs from findViewById?

问题

以下是翻译好的部分:

"在Android中的一个LayoutInflater示例代码:

View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.,parent,false);

无法理解LayoutInflater在Android中的工作原理。尝试阅读Android的官方文档,但无法理解概念。"

英文:

An example LayoutInflater code:

View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.,parent,false);

Can't understand the work of LayoutInflater in Android. Tried to read the official documentation of the android but, couldn't get the concept.

答案1

得分: 0

findViewById和LayoutInflater之间的主要区别在于,findViewById用于访问布局中的现有视图,而LayoutInflater用于从XML布局文件创建新的视图层次结构。

findViewById是Activity类的一个方法,而LayoutInflater是一个独立的类。

findViewById用于访问当前布局中的视图,这意味着我们可以从XML中查找视图,并返回对其Java对象的引用,而LayoutInflater用于从XML布局文件创建新的视图层次结构,因此该类负责“膨胀”布局。

希望这有所帮助。

英文:

The main difference between findViewById and LayoutInflater is that findViewById is used to access existing views in a layout, while LayoutInflater is used to create a new view hierarchy from an XML layout file.

FindViewById is a method of the Activity class, while LayoutInflater is a separate class on it's own.

FindViewById is used to access a view in the current layout, this means lets us find Views from layouts written in XML and returns a reference to their Java objects, while LayoutInflater is used to create a new view hierarchy from an XML layout file, so this class is the responsible for “inflating” the layouts.

Hope it helps.

huangapple
  • 本文由 发表于 2023年2月9日 01:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定