在Android中LayoutInflater的用途是什么?


当前回答

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

其他回答

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

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

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

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

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

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

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

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;
    }

}

}

LayoutInflator做什么?

当我第一次开始Android编程时,我真的对LayoutInflater和findViewById感到困惑。有时我们用一种,有时用另一种。

LayoutInflater用于从您的xml布局之一创建一个新的视图(或布局)对象。 findViewById只是给你一个已经创建的视图的引用。你可能认为你还没有创建任何视图,但无论何时你在onCreate中调用setContentView,活动的布局及其子视图在幕后被膨胀(创建)。

如果视图已经存在,那么使用findViewById。如果不是,那么使用LayoutInflater创建它。

例子

这是我做的一个小项目,显示LayoutInflater和findViewById在行动。在没有特殊代码的情况下,布局如下所示。

蓝色的正方形是一个自定义布局插入到主布局包含(见这里更多)。它是自动膨胀的,因为它是内容视图的一部分。如您所见,代码没有什么特别之处。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

现在让我们膨胀(创建)自定义布局的另一个副本并将其添加进来。

LayoutInflater inflater = getLayoutInflater();
View myLayout = inflater.inflate(R.layout.my_layout, mainLayout, false);

为了膨胀新的视图布局,我所做的只是告诉膨胀器我的xml文件的名称(my_layout),我想要添加它的父布局(mainLayout),以及我实际上还不想添加它(false)。(我也可以设置父为空,但然后我的自定义布局的根视图的布局参数将被忽略。)

这里是它的背景。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // inflate the main layout for the activity
        setContentView(R.layout.activity_main);

        // get a reference to the already created main layout
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_main_layout);

        // inflate (create) another copy of our custom layout
        LayoutInflater inflater = getLayoutInflater();
        View myLayout = inflater.inflate(R.layout.my_layout, mainLayout, false);

        // make changes to our custom layout and its subviews
        myLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
        TextView textView = (TextView) myLayout.findViewById(R.id.textView);
        textView.setText("New Layout");

        // add our custom layout to the main layout
        mainLayout.addView(myLayout);
    }
}

注意findViewById是如何仅在布局已经膨胀之后使用的。

附加的代码

下面是上面示例的xml。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_layout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- Here is the inserted layout -->
    <include layout="@layout/my_layout"/>

</LinearLayout>

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:textColor="@android:color/white"
        android:text="My Layout"/>

</RelativeLayout>

你什么时候需要LayoutInflater

大多数人最常使用它的时候是在RecyclerView中。(关于列表或网格,请参阅这些RecyclerView示例。)您必须为列表或网格中的每一个可见项目扩展一个新的布局。 如果您想以编程方式添加一个复杂的布局(就像我们在示例中所做的那样),您也可以使用布局膨胀器。您可以在代码中完成这一切,但是先用xml定义它,然后再对它进行膨胀要容易得多。