首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓系统中的LayoutInflater是做什么的?

安卓系统中的LayoutInflater是做什么的?
EN

Stack Overflow用户
提问于 2010-08-13 21:46:37
回答 11查看 299.8K关注 0票数 369

LayoutInflater在安卓系统中的用途是什么?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-08-13 22:11:41

ListView中使用自定义视图时,必须定义行布局。您创建了一个xml,在其中放置android小部件,然后在适配器的代码中,您必须执行以下操作:

代码语言:javascript
复制
public MyAdapter(Context context, List<MyObject> objects) extends ArrayAdapter {
  super(context, 1, objects);
  /* We get the inflator in the constructor */
  mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view;
  /* We inflate the xml which gives us a view */
  view = mInflater.inflate(R.layout.my_list_custom_row, parent, false);

  /* Get the item in the adapter */
  MyObject myObject = getItem(position);

  /* Get the widget with id name which is defined in the xml of the row */
  TextView name = (TextView) view.findViewById(R.id.name);

  /* Populate the row's xml with info from the item */
  name.setText(myObject.getName());

  /* Return the generated view */
  return view;
}

official documentation中阅读更多内容。

票数 166
EN

Stack Overflow用户

发布于 2010-08-13 21:54:02

LayoutInflater类用于将布局XML文件的内容实例化到其相应的View对象中。

换句话说,它接受一个XML文件作为输入,并从该文件构建View对象。

票数 316
EN

Stack Overflow用户

发布于 2014-03-17 05:14:06

LayoutInflater.inflate()提供了一种方法,可以将定义视图的res/layout/*.xml文件转换为应用程序源代码中可用的实际视图对象。

基本的两个步骤:获得充气器,然后充气资源

你怎么弄到充气装置的?

代码语言:javascript
复制
LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

假设xml文件是"list_item.xml“,如何获得视图?

代码语言:javascript
复制
View view = inflater.inflate(R.layout.list_item, parent, false);
票数 33
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3477422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档