您的位置:首页 > Web前端

The difference between LayoutInflater.inflate and findViewById

2015-09-12 09:30 453 查看
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
View
that
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: