有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性来布局,比为我们设置了android:layout_marginTop=”100dip”,但是运行程序后发现一点作用都没有,相似的还有layout_height等以android:layout_开头的属性设置都没有作用,这类问题以我们使用Adapter的作为数据源的时候作用居多,因为Adapter里有一个方法是getView,这个返回的VIew是一个从XML布局里加载的,一般如下:

[java]  view plain copy print ?
  1. if(convertView==null){  
  2. convertView=LayoutInflater.from(mContext).inflate(R.layout.main, null);  
  3. }  
  4. return convertView;  

      问题恰恰出在我们的LayoutInflater.from(mContext).inflate(R.layout.main, null);这句代码上,在使用inflate的时候,如果第二个参数(View root)为null,那么将不会加载你的布局文件里的最顶层的那个布局节点的布局相关配置(就是以android:layout_开头的属性)..我们可以看下该方法的实现来说明一下,通过查找源代码,inflate的实现都在这个public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) 方法里定义。。其中一段: [java]  view plain copy print ?
  1. if (root != null) {  
  2.    if (DEBUG) {  
  3.       System.out.println("Creating params from root: " +root);  
  4.    }  
  5.    // Create layout params that match root, if supplied  
  6.    params = root.generateLayoutParams(attrs);  
  7.    if (!attachToRoot) {  
  8.        // Set the layout params for temp if we are not  
  9.         // attaching. (If we are, we use addView, below)  
  10.         temp.setLayoutParams(params);  
  11.     }  
  12. }  


可以看到,当root为null的时候是不会执行params = root.generateLayoutParams(attrs);这段代码的,这段代码就是把xml里的布局配置转为LayoutParams,换句说就是加载我们配置的布局属性,以供布局类(FrameLayout等)在onLayout的时候控制View的大小、位置、对齐等等。。以FrameLayout为例,看下它的generateLayoutParams(attrs)方法。 [java]  view plain copy print ?
  1. public LayoutParams generateLayoutParams(AttributeSet attrs) {  
  2.         return new FrameLayout.LayoutParams(getContext(), attrs);          
  3.     }  

    很简单,构造了一个FrameLayout.LayoutParams类,该类集成了MarginParams,增加了一个gravity对其的属性配置。。。       在这里,如果要自定义自己的VIewroup,并且该ViewGroup有一些自定义控制布局的属性设置,就可以通过集成View.MarginParams来扩展布局配置,然后重写generateLayoutParams方法,这样系统框架就会自动使用该布局读取我们在xml中配置的布局属性来控制我们的VIew的位置。。       基于以上分析,我们在使用LayoutInflate的inflate方法的时候一定要保证root参数不能为null,其实这个root就是父View的意思,就是说你把xml转换为一个VIew的时候,该VIew的Parent是root,如果你不想把该View添加到该root里,那么让第三个参数 attachToRoot为false,如果要添加则为true.       说到这个问题了,其实还有一些布局,他们的参数配置要满足一定的条件才会起作用,比如FrameLayout里的View,你要想它的leftMargin生效,必须指定它的layout_gravity为left,同理right对应rightMargin.top和bottom也一样。。在使用时注意即可,多看看源代码。要不然就会莫名起名,不知道哪里的问题。 ViewGroup的三条线:
  1. onMeasure 测量View的大小
  2. onLayout 对View的布局进行控制
  3. draw绘制该View,drawChild绘制子VIew

更多相关文章

  1. android 自定义控件学习之三 控件布局常用知识总结
  2. Android软件广告屏蔽方法及代码
  3. Android异步处理常用方法
  4. shape基本用法及全部属性定义

随机推荐

  1. android*SimpleCursorAdapter *_id
  2. wzplayer for android V1.5.3 (新增YUV文
  3. Android分享到腾讯微博,信息,新浪微博等等,
  4. Android(安卓)Notification的基本使用和
  5. Android下实现数据绑定功能
  6. Android(安卓)面向接口编程
  7. android 对话框模板布局之一
  8. 让android的LogCat支持中文输出
  9. android 从SIM卡获取联系人信息
  10. ScrollView 如何恒显示滚动条