Android应用程序开发中的充气机

Dav*_*own 10 android

任何人都可以告诉什么是Inflator是什么以及如何在Android应用程序中使用它?

我不知道它的确切用法以及为什么使用它.

Cha*_*s B 21

我处理通胀的首选方式:

//First get our inflater ready, you'll need the application/activity context for this
LayoutInflater mInflater;
mInflater = LayoutInflater.from(mContext);

//Inflate the view from xml
View newView = mInflater.inflate(R.layout.my_new_layout, null);

//Then you'll want to add it to an existing layout object
mMainLayout.add(newView);

//Or perhaps just set it as the main view (though this method can also 
// inflate the XML for you if you give it the resource id directly)
setContentView(newView);
Run Code Online (Sandbox Code Playgroud)

基本上,您可以在运行时使用它来扩充现有的xml布局.通常,您可以将这些新视图插入到先前定义的ViewGroups或List对象中.


Vid*_*nes 6

不太清楚你的意思,但如果它与膨胀视图有关,它用于将布局xml文件加载到你的应用程序中.例如

View myWelcome = View.inflate(this, R.layout.welcome, null);
Run Code Online (Sandbox Code Playgroud)

您可以更轻松地考虑最佳实践,让您在布局xml文件中查看定义,而不是通过代码完全创建视图.