LayoutInflater
在安卓系统中的用途是什么?
发布于 2010-08-13 22:11:41
在ListView
中使用自定义视图时,必须定义行布局。您创建了一个xml,在其中放置android小部件,然后在适配器的代码中,您必须执行以下操作:
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中阅读更多内容。
发布于 2010-08-13 21:54:02
LayoutInflater类用于将布局XML文件的内容实例化到其相应的View对象中。
换句话说,它接受一个XML文件作为输入,并从该文件构建View对象。
发布于 2014-03-17 05:14:06
LayoutInflater.inflate()提供了一种方法,可以将定义视图的res/layout/*.xml文件转换为应用程序源代码中可用的实际视图对象。
基本的两个步骤:获得充气器,然后充气资源
你怎么弄到充气装置的?
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
假设xml文件是"list_item.xml“,如何获得视图?
View view = inflater.inflate(R.layout.list_item, parent, false);
https://stackoverflow.com/questions/3477422
复制相似问题