android - 从 xml 文件中扩充 View 是什么意思?

标签 android android-inflate

我是 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 。在这种情况下,您必须:

  1. 获取 LayoutInflater
  2. 的实例
  3. 指定要扩展的 XML
  4. 使用返回的查看
  5. 使用返回的 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/

相关文章:

android - 将带有 RadioButton 的嵌套布局扩展到容器

android - 更改导航时 map fragment 崩溃

java - 获取拨号器 Intent 的 RESULT_CANCELED

android - sencha touch package build - 只显示命令提示符不显示任何错误

android - LayoutInflater是不是每次都加载xml?

android - 错误膨胀类 ImageButton 停止应用程序启动

android - 显示上一个 fragment

android - 带有多个 Firebase 的 Flutter 应用程序(适用于多环境)

android - 渲染 View 以查看寻呼机 - 优化方式

android - NPE 同时 inflated layout (尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.equals(java.lang.Object)')