• Android编程之LayoutInflater的inflate方法详解


    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到:

    [java] view plaincopy
     
    1. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    2.         Bundle savedInstanceState) {  


    LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:

    [java] view plaincopy
     
    1. public View inflate(int resource, ViewGroup root) {  
    2.     return inflate(resource, root, root != null);  
    3. }  
    [java] view plaincopy
     
    1. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {  
    2.     if (DEBUG) System.out.println("INFLATING from resource: " + resource);  
    3.     XmlResourceParser parser = getContext().getResources().getLayout(resource);  
    4.     try {  
    5.         return inflate(parser, root, attachToRoot);  
    6.     } finally {  
    7.         parser.close();  
    8.     }  
    9. }  

    所以,这里直接介绍里面调用这个方法即可:

    [java] view plaincopy
     
    1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)  


    在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:

    [java] view plaincopy
     
    1. ViewGroup.LayoutParams params = null;  
    2.   
    3. if (root != null) {  
    4.     if (DEBUG) {  
    5.         System.out.println("Creating params from root: " +  
    6.                 root);  
    7.     }  
    8.     // Create layout params that match root, if supplied  
    9.     params = root.generateLayoutParams(attrs);  
    10.     if (!attachToRoot) {  
    11.         // Set the layout params for temp if we are not  
    12.         // attaching. (If we are, we use addView, below)  
    13.         temp.setLayoutParams(params);  
    14.     }  
    15. }  

    params = root.generateLayoutParams(attrs);

    这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。

    接着往下看:

    [java] view plaincopy
     
    1. // We are supposed to attach all the views we found (int temp)  
    2. // to root. Do that now.  
    3. if (root != null && attachToRoot) {  
    4.     root.addView(temp, params);  
    5. }  
    6.   
    7. // Decide whether to return the root that was passed in or the  
    8. // top view found in xml.  
    9. if (root == null || !attachToRoot) {  
    10.     result = temp;  
    11. }  

    root.addView(temp, params);

    如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。

    如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。

    以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个例子,来具体说明一下。

    如果你不关心其内部实现,只看如何使用的话,直接看这篇即可。

    接上篇,接下来,就用最最简单的例子来说明一下:

    用两个布局文件main 和 test:

    其中,main.xml文件为:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <TextView  
    8.         android:layout_width="match_parent"  
    9.         android:layout_height="50dp"  
    10.         android:gravity="center"  
    11.         android:text="hello world" />  
    12.   
    13. </LinearLayout>  


    test.xml文件为:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="200dp"  
    5.     android:background="#ffffff00"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TextView  
    9.         android:layout_width="match_parent"  
    10.         android:layout_height="50dp"  
    11.         android:gravity="center"  
    12.         android:text="test" />  
    13.   
    14. </LinearLayout>  


    在test中设置了其高度为200dp,并且设置了背景颜色。

    接下来看一下LayoutInflater().inflate方法实现:

    第一种方式:inflate(view, null)

    [java] view plaincopy
     
    1. @Override  
    2. protected void onCreate(Bundle savedInstanceState) {  
    3.     super.onCreate(savedInstanceState);  
    4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
    5.             null);  
    6.   
    7.     view = getLayoutInflater().inflate(R.layout.test, null);  
    8.   
    9.     setContentView(view);  
    10. }  

    运行的效果如下:

    这个就很容易理解了,因为我没有指定ViewGroup root参数,所以,相当于直接加载了test视图文件,并返回。

    而它的高度充满了全屏而不是200dp,因为执行inflate的时候,没有root参数,则无法为test视图设定layoutparam参数。那么为什么会充满屏幕而不是没有显示呢?是因为我们将其设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。有兴趣的话,你可以改一下test的layout_width设定一个数值,最后运行效果是一样的。

    第二种方式:inflate(view, root, false)

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. @Override  
    2. protected void onCreate(Bundle savedInstanceState) {  
    3.     super.onCreate(savedInstanceState);  
    4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
    5.             null);  
    6.   
    7.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view, false);  
    8.   
    9.     setContentView(view);  
    10. }  


    这里调用inflate的时候,强转了view为viewgroup,因为其本身就是linearlayout,所以这里可以强转。

    运行的效果如下:

    单看效果而言,跟上面的一样。但从代码本身而言,实现的内容就不一样了。由于有了viewgroup,这里得到的视图其实已经有了layoutparam,你可以自行打印Log看看。

    但为什么最后的结果却是和上面的一样呢。原因还是由于设置视图到activity时,会取得当前window的layoutparam赋值给它,也就是充满全屏。

    第三种方式:inflate(view, root, true)

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. @Override  
    2. protected void onCreate(Bundle savedInstanceState) {  
    3.     super.onCreate(savedInstanceState);  
    4.     View view = (LinearLayout) getLayoutInflater().inflate(R.layout.main,  
    5.             null);  
    6.   
    7.     view = getLayoutInflater().inflate(R.layout.test, (ViewGroup) view,  
    8.             true);  
    9.   
    10.     setContentView(view);  
    11. }  

    运行的效果如下:

    这个效果就很明显了,由于main是线性布局,所以,test视图被添加到了textview(hello world)下面,并且保留了其自己的layoutparam参数。

    例子很简单,就不附上代码工程。

    如果对inflate方法如何实现的,感兴趣的话,可以参考上一篇文章:

    Android编程之LayoutInflater的inflate方法详解

    补充:新的API会在inflater.inflate(R.layout.xxx, null);提示错误:

  • 相关阅读:
    golang 使用 os/exec
    golang调用 exec命令 出现too many open files
    nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”
    Docker 实时查看docker容器日志
    curl 查看HTTP 响应头信息
    go -- go 程序 启动docker容器
    go -- 测试
    Linux /var/log下各种日志文件
    LDAP -- ldap 的cn, ou, dc的含义
    一条命令 杀死某个端口 所有程序
  • 原文地址:https://www.cnblogs.com/bdbw2012/p/4673565.html
Copyright © 2020-2023  润新知