博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
layoutInflater 获取布局对象
阅读量:7091 次
发布时间:2019-06-28

本文共 3435 字,大约阅读时间需要 11 分钟。

hot3.png

 

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面
元素。
 
首先 我们 要知道,layoutInflater 是个抽象类,获得其实例有3种方法:
 
 LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater() 
 LayoutInflater inflater = LayoutInflater.from(context);// 从这个上下文context获取LayoutInflater 
 LayoutInflater inflater =  (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
                                                                                           
//取得.xml 里定义的view
从源码来看这3个方法的实质是一样的。
getLayoutInflater() 是调用Activity类的一个方法 ,是调用 PhoneWindow 的getLayoutInflater()方法。源码:
public PhoneWindow(Context context)
{   
     super(context);   
    mLayoutInflater = LayoutInflater.from(context);
}
可以知道,其中调用了  LayoutInflater.from(context);而LayoutInflater.from(context)的源码;
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;
}
可以看出它其实调用 context.getSystemService()。
 

结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

getSystemService() 是android 一个重要的api接口,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。

传入的Name                                                       
返回的对象                                                 
说明
WINDOW_SERVICE                                          WindowManager                               管理打开的窗口程序
LAYOUT_INFLATER_SERVICE                              LayoutInflater                                  取得xml里定义的view
ACTIVITY_SERVICE                                            ActivityManager                              管理应用程序的系统状态
POWER_SERVIC                                                 E PowerManger                                       电源的服务
ALARM_SERVICE                                               AlarmManager                                         闹钟的服务
NOTIFICATION_SERVICE                              NotificationManager                                  状态栏的服务
KEYGUARD_SERVICE                                        KeyguardManager                                   键盘锁的服务
LOCATION_SERVICE                                          LocationManager                                位置的服务,如GPS
SEARCH_SERVICE                                               SearchManager                                        搜索的服务
VEBRATOR_SERVICE                                            Vebrator                                            手机震动的服务
CONNECTIVITY_SERVICE                                    Connectivity                                           网络连接的服务
WIFI_SERVICE                                                     WifiManager                                            Wi-Fi服务
TELEPHONY_SERVICE                                     TeleponyManager                                         电话服务
 

layoutInlater一般会和inflate()使用,inlate的英文意思为:扩展。

在sdk文档里,他有4种重载方式,返回值均是 View 对象。

public View inflate (int resource, ViewGroup root) 
//reSourceViewlayoutID,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)

示例:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       
 
 inflater(int resource, root)
inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。

 示例:

比如说当前有一个项目,其中MainActivity对应的布局文件叫做activity_main.xml, 如下所示:
<
LinearLayout
 
xmlns:android
=
"http://schemas.android.com/apk/res/android"
  
    
android:id
=
"@+id/main_layout"
  
    
android:layout_width
=
"match_parent"
  
    
android:layout_height
=
"match_parent"
 
>
  
  
</
LinearLayout
>
 

 再定义一个布局文件,给它取名为button_layout.xml,代码如下所示:

<
Button
 
xmlns:android
=
"http://schemas.android.com/apk/res/android"
  
    
android:layout_width
=
"wrap_content"
  
    
android:layout_height
=
"wrap_content"
  
    
android:text
=
"Button"
 
>
  
  
</
Button
>
  改MainActivity中的代码
public
 
class
 MainActivity 
extends
 Activity {  
  
    
private
 LinearLayout mainLayout;  
  
    
@Override
  
    
protected
 
void
 onCreate(Bundle savedInstanceState) {  
        
super
.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mainLayout = (LinearLayout) findViewById(R.id.main_layout);  
        LayoutInflater layoutInflater = LayoutInflater.from(
this
);  
        View buttonLayout = layoutInflater.inflate(R.layout.button_layout, 
null
);  
        mainLayout.addView(buttonLayout);  
    }  
  
}  

 

转载于:https://my.oschina.net/u/1773495/blog/305931

你可能感兴趣的文章
Quartz 开源任务调度框架
查看>>
SASS界面编译工具——Koala的使用
查看>>
JSP放入Jar包支持
查看>>
润乾报表使用json数据源的方法改进
查看>>
小蚂蚁学习PS切图之基础操作(2)——工具栏的介绍
查看>>
【Mybatis】- sqlSession工作流程
查看>>
mysql str_to_date字符串转换为日期
查看>>
jsp---EL运算符
查看>>
Oracle中的substr方法
查看>>
Mysql日期和时间函数总结
查看>>
创建逻辑卷 安装lvm命令
查看>>
不使用root身份运行Wireshark
查看>>
PageRank算法计算网页的价值
查看>>
js面向对象
查看>>
DEDECMS 修改广告链接地址
查看>>
抓住“扁平化”
查看>>
Python中method的参数传递详解
查看>>
Skia深入分析1——skia上下文
查看>>
Tiny Jpeg Decoder (JPEG解码程序) 源代码分析 1:解码文件头
查看>>
windows Server2008 下部署nginx
查看>>