在Android中LayoutInflater的用途是什么?
当前回答
LayoutInflater类用于将布局XML文件的内容实例化到相应的View对象中。
换句话说,它接受一个XML文件作为输入,并从中构建View对象。
其他回答
LayoutInflater类用于将布局XML文件的内容实例化到相应的View对象中。
换句话说,它接受一个XML文件作为输入,并从中构建View对象。
下面是另一个与前一个类似的示例,但扩展了它以进一步演示它可以提供的膨胀参数和动态行为。
假设你的ListView行布局可以有可变数量的textview。因此,首先膨胀基本项视图(就像前面的例子一样),然后在运行时动态循环添加textview。使用android:layout_weight额外对齐一切完美。
以下是布局资源:
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/field1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<TextView
android:id="@+id/field2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
schedule_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
重写BaseAdapter类扩展中的getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View lst_item_view = inflater.inflate(R.layout.list_layout, null);
TextView t1 = (TextView) lst_item_view.findViewById(R.id.field1);
TextView t2 = (TextView) lst_item_view.findViewById(R.id.field2);
t1.setText("some value");
t2.setText("another value");
// dinamically add TextViews for each item in ArrayList list_schedule
for(int i = 0; i < list_schedule.size(); i++){
View schedule_view = inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false);
((TextView)schedule_view).setText(list_schedule.get(i));
((ViewGroup) lst_item_view).addView(schedule_view);
}
return lst_item_view;
}
注意不同的膨胀方法调用:
inflater.inflate(R.layout.list_layout, null); // no parent
inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false); // with parent preserving LayoutParams
充气机的作用
它接受一个xml布局作为输入(比如说)并将其转换为视图对象。
为什么需要
让我们设想一个场景,我们需要创建一个自定义列表视图。现在每一行都是自定义的。但是我们怎么做呢?不可能将xml布局分配给列表视图的一行。我们创建了一个View对象。因此,我们可以访问其中的元素(textview,imageview等),也分配对象作为列表视图行
无论何时我们需要在某处分配视图类型对象我们有自定义xml设计我们只需将它转换为object by inflater并使用它。
LayoutInflater是Android的一个基本组件。您必须一直使用它来将xml文件转换为视图层次结构。
LayoutInflater基于XML中定义的布局创建View对象。有几种不同的方法来使用LayoutInflater,包括创建自定义视图,将片段视图膨胀为活动视图,创建对话框,或简单地将布局文件视图膨胀为活动。
关于暴胀过程是如何运作的,有很多误解。我认为这是因为inflation()方法的文档很差。如果你想详细了解inflation()方法,我在这里写了一篇关于它的博客文章:
https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。