1. 基本概念

1. 概念:

参考资料:https://www.cnblogs.com/androidez/archive/2013/07/01/3164729.html

一个用于加载布局的系统服务,就是实例化与Layout XML文件对应的View对象,不能直接使用, 需要通过getLayoutInflater( )方法或getSystemService( )方法来获得与当前Context绑定的 LayoutInflater实例;记住:他是一个服务!!

LayoutInflater这个类它的作用类似于findViewById()。

不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

说白了就是用来动态加载控件的服务

2. 获得 LayoutInflater 实例的三种方式:

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   //从这里可以看出来他是一个服务

3. LayoutInflater inflater = LayoutInflater.from(context);     //很多时候用这一种

2. 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aplex_new1.myapplication.MainActivity"> </android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_center"
android:background="@color/colorAccent"
android:gravity="center"
tools:context="com.example.aplex_new1.myapplication.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflat 测试"/> </LinearLayout>
public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到这个界面
//LinearLayout linerlayout = findViewById(R.id.id_center);
LayoutInflater inflater = LayoutInflater.from(this);
//找到test_inflat.xml并且实例化
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.test_inflat,null, false);
//.findViewById(R.id.id_center);
//将已经实例化的布局显示出来
setContentView(ll);
}
}

Android服务--布局服务(LayoutInflater)的更多相关文章

  1. Android调用Web服务

    现在大部分应用程序都把业务逻辑处理,数据调用等功能封装成了服务的形式,应用程序只需要调用这些web服务就好了,在这里就不赘述web服务的优点了.本文总结如何在android中调用Web服务,通过传递基 ...

  2. Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  3. Android窗口管理服务WindowManagerService显示窗口动画的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8611754 在前一文中,我们分析了Activi ...

  4. Android窗口管理服务WindowManagerService切换Activity窗口(App Transition)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8596449 在Android系统中,同一时刻只 ...

  5. Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8577789 在Android系统中,Activ ...

  6. Android窗口管理服务WindowManagerService计算窗口Z轴位置的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8570428 通过前面几篇文章的学习,我们知道了 ...

  7. Android窗口管理服务WindowManagerService对壁纸窗口(Wallpaper Window)的管理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8550820 Android系统中,壁纸窗口和输 ...

  8. Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8526644 在Android系统中,输入法窗口 ...

  9. Android中的服务

    Android中的服务 四大组件都是运行在主线程 Android中的服务,是在后台运行 .可以理解成是在后台运行并且是没有界面的Activity. Foreground process 前台进程 ,用 ...

随机推荐

  1. django drf django-filter的method过滤

    1.View Demo from django.shortcuts import render from rest_framework.views import APIView from rest_f ...

  2. .net程序员书单

    C# 基础 <CLR via C#> <c# 高级编程> 框架学习 <WPF编程宝典 > (英文名:<Pro WPF 4.5 in C#. Windows P ...

  3. python--区分函数和方法, 反射

    1.  isinstance,   type,   issubclass isinstance(): 判断你给的xxx对象是否是xxxxx类型的,只支持向上判断 isinstance(object, ...

  4. ssh 使用密钥无法登入Linux系统

    今天测试密钥登入linux系统时 出现如下问题: root@compute01:~# ssh alicxxx@xxx.com -p -i alickicxxxxxxx.key @@@@@@@@@@@@ ...

  5. A - 畅通工程 (并查集)

    点击打开链接 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连 ...

  6. TPS和QPS定义以及影响TPS的因素

    一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...

  7. PrefixLength的计算

    直接用android4.2.2的源码来说话: \frameworks\base\services\java\com\android\server\NetworkManagementService.ja ...

  8. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...

  9. Java_多线程2_线程池

    线程池(pool): 线程池的作用: 1.节省资源,减少线程的数量和创建销毁线程的开销2.合理的管理线程的分配 线程池的创建: 1.newCachedThreadPool //优点:很灵活,弹性的线程 ...

  10. 架构师养成记--33.Redis哨兵、redis简单事务

    Redis哨兵 有了主从复制,如果我想想对主从服务器进行监控,在redis2.6后提供了哨兵机制,2.6有哨兵1.0版本,并不稳定.2.8以后的哨兵功能才稳定起来. 顾名思义,哨兵就是监控Redis系 ...