在Android中LayoutInflater的用途是什么?


当前回答

当你在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;
}

在官方文档中阅读更多信息。

其他回答

LayoutInflater基于XML中定义的布局创建View对象。有几种不同的方法来使用LayoutInflater,包括创建自定义视图,将片段视图膨胀为活动视图,创建对话框,或简单地将布局文件视图膨胀为活动。

关于暴胀过程是如何运作的,有很多误解。我认为这是因为inflation()方法的文档很差。如果你想详细了解inflation()方法,我在这里写了一篇关于它的博客文章:

https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/

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

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

Layout inflater是一个读取xml外观描述并将其转换为基于java的View对象的类。

我的定制列表希望它能说明概念

public class second extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
//  TextView textview=(TextView)findViewById(R.id.textView1);
//  textview.setText(getIntent().getExtras().getString("value"));

    setListAdapter(new MyAdapter(this,R.layout.list_item,R.id.textView1, getResources().getStringArray(R.array.counteries)));
}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] objects) {
        super(context, resource, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.list_item,parent,false);
        String[]items=getResources().getStringArray(R.array.counteries);

        ImageView iv=(ImageView) row.findViewById(R.id.imageView1);
        TextView tv=(TextView) row.findViewById(R.id.textView1);
        tv.setText(items[position]);

        if(items[position].equals("unitedstates")){
            iv.setImageResource(R.drawable.usa);
        }else   if(items[position].equals("Russia")){
            iv.setImageResource(R.drawable.russia);
        }else   if(items[position].equals("Japan")){
            iv.setImageResource(R.drawable.japan);
        }
        // TODO Auto-generated method stub
        return row;
    }

}

}

Inflater实际上是将数据,视图,实例转换为可见的UI表示.. ..因此,它以编程的方式利用来自适配器等的数据馈送。然后将它与你定义的xml集成,告诉它数据应该如何在UI中表示