Android数据填充器LayoutInflater

简介:

 LayoutInflater类在应用程序中比较实用,可以叫布局填充器,也可以成为打气筒,意思就是将布局文件填充到自己想要的位置,LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化这个XML文件成为一个View,有点类似于类似于findViewById(),但是findViewById()是找xml布局文件下的具体widget控件(Button、TextView)。对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;对于一个已经载入的界面,使用Activiyt.findViewById()方法来获得其中的界面元素

LayoutInflater实例方式

首先简单看下三种简单的实例方式:

1
2
3
LayoutInflater layoutInflater = getLayoutInflater();
LayoutInflater layoutInflater2 = LayoutInflater.from( this );
LayoutInflater layoutInflater3 = (LayoutInflater)  this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

第一种getLayoutInflater方式:

1
2
3
public  LayoutInflater getLayoutInflater() {
      return  getWindow().getLayoutInflater();
  }

第二种实现方式:

1
2
3
4
5
6
7
8
public  static  LayoutInflater from(Context context) {
     LayoutInflater LayoutInflater =
             (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     if  (LayoutInflater ==  null ) {
         throw  new  AssertionError( "LayoutInflater not found." );
     }
     return  LayoutInflater;
}

 后两种调用的都是getSystemService,第一种也是~

LayoutInflater实现Demo

LayOutInflater填充布局的时候有四种方法,Demo使用第一种:

resource:需要加载布局文件的id,需要将这个布局文件中加载到Activity中来操作。

root:inflate()会返回一个View对象如果root不为null,就将这个root作为根对象返回,作为这个xml文件的根视图,如果是null,那么这个xml文件本身就是一个根视图:

1
2
3
4
5
6
public  View inflate ( int  resource, ViewGroup root) 
public  View inflate (XmlPullParser parser, ViewGroup root) 
   
public  View inflate (XmlPullParser parser, ViewGroup root,  boolean  attachToRoot) 
   
public  View inflate ( int  resource, ViewGroup root,  boolean  attachToRoot)

 看下实现的结果:

 

在主窗体中定义一个按钮,这个代码就不贴了~直接写点击事件的处理吧:

定义一个Item文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:id= "@+id/test"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
 
     <TextView
         android:id= "@+id/inflate_txt"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:text= "测试" />
 
</LinearLayout>

  点击事件的处理:

1
2
3
4
5
6
7
8
9
10
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.item,  null );
TextView text = (TextView) view.findViewById(R.id.inflate_txt);
text.setText( "http://www.cnblogs.com/xiaofeixiang" );
text.setTextSize( 18 );
text.setTextColor(Color.RED);
AlertDialog.Builder builder =  new  AlertDialog.Builder(MainActivity. this );
builder.setView(view);
AlertDialog  alertDialog = builder.create();
alertDialog.show();
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4060705.html,如需转载请自行联系原作者


相关文章
|
5月前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
54 1
|
2月前
|
开发工具 Android开发 开发者
Android平台如何不推RTMP|不发布RTSP流|不实时录像|不回传GB28181数据时实时快照?
本文介绍了一种在Android平台上实现实时截图快照的方法,尤其适用于无需依赖系统接口的情况,如在RTMP推送、RTSP服务或GB28181设备接入等场景下进行截图。通过底层模块(libSmartPublisher.so)实现了截图功能,封装了`SnapShotImpl.java`类来管理截图流程。此外,提供了关键代码片段展示初始化SDK实例、执行截图、以及在Activity销毁时释放资源的过程。此方案还考虑到了快照数据的灵活处理需求,符合GB/T28181-2022的技术规范。对于寻求更灵活快照机制的开发者来说,这是一个值得参考的设计思路。
|
4月前
|
XML 存储 JSON
51. 【Android教程】JSON 数据解析
51. 【Android教程】JSON 数据解析
96 2
|
5月前
|
数据库 Android开发
Android 通过升级SettingsProvider数据强制覆盖用户的设置项
Android 通过升级SettingsProvider数据强制覆盖用户的设置项 【5月更文挑战第7天】
122 5
|
5月前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
72 2
|
5月前
|
Java Linux API
统计android设备的网络数据使用量
统计android设备的网络数据使用量
104 0
|
2月前
|
JSON Java Android开发
Android 开发者必备秘籍:轻松攻克 JSON 格式数据解析难题,让你的应用更出色!
【8月更文挑战第18天】在Android开发中,解析JSON数据至关重要。JSON以其简洁和易读成为首选的数据交换格式。开发者可通过多种途径解析JSON,如使用内置的`JSONObject`和`JSONArray`类直接操作数据,或借助Google提供的Gson库将JSON自动映射为Java对象。无论哪种方法,正确解析JSON都是实现高效应用的关键,能帮助开发者处理网络请求返回的数据,并将其展示给用户,从而提升应用的功能性和用户体验。
50 1
|
2月前
|
缓存 API Android开发
Android经典实战之Kotlin Flow中的3个数据相关的操作符:debounce、buffer和conflate
本文介绍了Kotlin中`Flow`的`debounce`、`buffer`及`conflate`三个操作符。`debounce`过滤快速连续数据,仅保留指定时间内的最后一个;`buffer`引入缓存减轻背压;`conflate`仅保留最新数据。通过示例展示了如何在搜索输入和数据流处理中应用这些操作符以提高程序效率和用户体验。
40 6
|
2月前
|
编解码 网络协议 前端开发
如何实现Android平台GB28181设备接入模块按需打开摄像头并回传数据
后台采集摄像头,如果想再进一步扩展,可以把android平台gb28181的camera2 demo,都移植过来,实现功能更强大的国标设备侧,这里主要是展示,收到国标平台侧的回传请求后,才打开摄像头,才开始编码打包,最大限度的减少资源的占用
|
2月前
|
编解码 网络协议 Android开发
Android平台GB28181设备接入模块实现后台service按需回传摄像头数据到国标平台侧
我们在做Android平台GB28181设备对接模块的时候,遇到这样的技术需求,开发者希望能以后台服务的形式运行程序,国标平台侧没有视频回传请求的时候,仅保持信令链接,有发起视频回传请求或语音广播时,打开摄像头,并实时回传音视频数据或接收处理国标平台侧发过来的语音广播数据。
下一篇
无影云桌面