我是 android 开发的新手,经常从布局 xml 文件中看到对 Inflating View 的引用。我用谷歌搜索并搜索了开发指南,但仍然无法理解它的含义。如果有人能提供一个非常简单的例子,将不胜感激。
最佳答案
当你编写一个 XML 布局时,它会被 Android 操作系统膨胀,这基本上意味着它将通过在内存中创建 View 对象来呈现。我们称之为隐式膨胀(操作系统会为您膨胀 View )。例如:
class Name extends Activity{
public void onCreate(){
// the OS will inflate the your_layout.xml
// file and use it for this activity
setContentView(R.layout.your_layout);
}
}
您还可以使用 LayoutInflater
显式扩充 View 。在这种情况下,您必须:
- 获取
LayoutInflater
的实例
- 指定要扩展的 XML
- 使用返回的
查看
- 使用返回的 View 设置内容 View (上)
例如:
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
setContentView(theInflatedView) // 4
关于android - 从 xml 文件中扩充 View 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4576330/