What is the difference between getting a reference to a widget like this:
TableRow row = findViewById(R.id.table_row);
and:
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
Is there also a difference when the TableRow is
a root of its layout or if its just a small part of a layout?
1) Using
TableRow row = findViewById(R.id.table_row);
you are simply obtaining a reference to a View with
id R.id.table_row which has already been created and inflated in the current layout (where current means the Activtiy's
layout or the Viewthat
you are defining).
2) Using
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
You are inflating (which means creating) a new views hierarchy based on the XML definition contained in R.layout.table_row. Since you are not passing the parent View parameter in the inflate()method, you will need to add the resulting hierarchy manually to an existent container.
理解Android布局中TableRow引用方式的区别
本文深入探讨了在Android应用开发中使用TableRow时,两种不同引用方式的区别:通过findViewById()获取已创建并初始化的视图实例,与通过LayoutInflater.from()进行视图的创建与加载。特别关注了当TableRow作为布局的根节点或其内部组件时,这两种方法的差异与应用场景。

被折叠的 条评论
为什么被折叠?



