Android提供的系统服务之--LayoutInflater(布局服务)

Android提供的系统服务之--LayoutInflater(布局服务)

                                                          ——转载请注明出处:coder-pig


本节引言:

本节我们只要是介绍Android系统服务中的——LayoutInflater(布局服务),

相信大家已经习惯了通过Activity.setContentView( )方法来我们的布局文件,

底层的底层还是通过这个系统的LayoutInflater来完成的!

工作原理就是使用Android内置的pull解析器来解析布局~

在前几天写的blog中用到的动态布局,就是用到这个东东,有兴趣的可以看看

纯Java代码加载布局:http://blog.csdn.net/coder_pig/article/details/43983709

Java代码动态添加组件或布局:http://blog.csdn.net/coder_pig/article/details/43988511

其实基本的用法示例就在上面这两篇blog中,本节就是一些概念的唠叨~




本节正文:

1.LayoutInflater是什么鬼?

答:一个用于加载布局的系统服务!就是实例化与Layout XML文件对应的View对象,不能直接使用,

需要通过getLayoutInflater( )方法或者getSystemService( )方法来获得与当前Context绑定的

LayoutInflater实例!



2.LayoutInflater的用法:

①获取LayoutInflater的三种方法:

[java]   view plain  copy  print ? 在CODE上查看代码片 派生到我的代码片
  1. //方法一  
  2. LayoutInflater inflater1 = LayoutInflater.from(this);  
  3. //方法二  
  4. LayoutInflater inflater2 = getLayoutInflater();  
  5. //方法三  
  6. LayoutInflater inflater3 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  


ps:其实前两个方法底层的代码都是调用的第三个东东~



②加载布局的方法:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

该方法的三个参数依次为:

①要加载的布局Id

②为该布局的外部再嵌套一层父布局,如果不需要的话,写null就可以了!

③是否为加载的布局文件的最外层套一层root布局,不设置该参数的话,如果

root不为null的话,默认为true,另外,如果root为null的话,attachToRoot就没有作用了!

root不为null,attachToRoot为true的话,会在加载的布局文件最外层嵌套一层root布局;

为false的话,则root失去作用!

简单点理解:是否为加载的布局添加一个root的外层容器~!




③通过LayoutInflater.LayoutParams来设置相关的属性:

比如RelativeLayout还可以通过addRule方法添加规则,就是设置位置,是参考父容器呢,

还是参考子控件,又或者设置margin等等,这个由你决定~

这里写个简单的例子吧:

activity_main.xml:

[html]   view plain  copy  print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.test.MainActivity"   
  6.     android:id="@+id/rly">  
  7.   
  8.     <Button  
  9.         android:id="@+id/btnOne"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="我是setContentView( )加载的Button" />  
  13.   
  14. </RelativeLayout>  

MainActivity.java:

[java]   view plain  copy  print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.example.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.Button;  
  7. import android.widget.RelativeLayout;  
  8. import android.widget.RelativeLayout.LayoutParams;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         Button btnTwo = new Button(this);  
  17.         btnTwo.setText("我是动态加载的Button");  
  18.         RelativeLayout rly = (RelativeLayout) findViewById(R.id.rly);  
  19.         LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  20.         RelativeLayout.LayoutParams lp = new LayoutParams(  
  21.                 RelativeLayout.LayoutParams.MATCH_PARENT,  
  22.                 RelativeLayout.LayoutParams.WRAP_CONTENT);  
  23.         lp.addRule(RelativeLayout.BELOW,R.id.btnOne);  
  24.         lp.setMargins(10101010);  
  25.         //addView( )方法可以设置不同参数哦~  
  26.         rly.addView(btnTwo, lp);  
  27.     }  
  28. }  


运行效果图:





代码是比较简单的,就不多解释了~,最后提供下LayoutInflater的inflate( )方法的源码,

有兴趣的可以研究下:

[java]   view plain  copy  print ? 在CODE上查看代码片 派生到我的代码片
  1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {    
  2.     synchronized (mConstructorArgs) {    
  3.         final AttributeSet attrs = Xml.asAttributeSet(parser);    
  4.         mConstructorArgs[0] = mContext;    
  5.         View result = root;    
  6.         try {    
  7.             int type;    
  8.             while ((type = parser.next()) != XmlPullParser.START_TAG &&    
  9.                     type != XmlPullParser.END_DOCUMENT) {    
  10.             }    
  11.             if (type != XmlPullParser.START_TAG) {    
  12.                 throw new InflateException(parser.getPositionDescription()    
  13.                         + ": No start tag found!");    
  14.             }    
  15.             final String name = parser.getName();    
  16.             if (TAG_MERGE.equals(name)) {    
  17.                 if (root == null || !attachToRoot) {    
  18.                     throw new InflateException("merge can be used only with a valid "    
  19.                             + "ViewGroup root and attachToRoot=true");    
  20.                 }    
  21.                 rInflate(parser, root, attrs);    
  22.             } else {    
  23.                 View temp = createViewFromTag(name, attrs);    
  24.                 ViewGroup.LayoutParams params = null;    
  25.                 if (root != null) {    
  26.                     params = root.generateLayoutParams(attrs);    
  27.                     if (!attachToRoot) {    
  28.                         temp.setLayoutParams(params);    
  29.                     }    
  30.                 }    
  31.                 rInflate(parser, temp, attrs);    
  32.                 if (root != null && attachToRoot) {    
  33.                     root.addView(temp, params);    
  34.                 }    
  35.                 if (root == null || !attachToRoot) {    
  36.                     result = temp;    
  37.                 }    
  38.             }    
  39.         } catch (XmlPullParserException e) {    
  40.             InflateException ex = new InflateException(e.getMessage());    
  41.             ex.initCause(e);    
  42.             throw ex;    
  43.         } catch (IOException e) {    
  44.             InflateException ex = new InflateException(    
  45.                     parser.getPositionDescription()    
  46.                     + ": " + e.getMessage());    
  47.             ex.initCause(e);    
  48.             throw ex;    
  49.         }    
  50.         return result;    
  51.     }    
  52. }    



那么,本节就到这里,如果你有什么补充的话欢迎提出~O(∩_∩)O谢谢~



参考资料:

郭大叔的blog:http://blog.csdn.net/guolin_blog/article/details/12921889

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值