`

Android Layout布局文件里的android:layout_height等属性为什么会不起作用?

阅读更多

 

有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性

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

 

if(convertView==null){
				convertView=LayoutInflater.from(mContext).inflate(R.layout.main, null);
			}
			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) 方法里定义。。其中一段:
               if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }
 可以看到,当root为null的时候是不会执行params = root.generateLayoutParams(attrs);这段代码的,这段代码就是把xml里的布局配置转为LayoutParams,换句说就是加载我们配置的布局属性,以供布局类(FrameLayout等)在onLayout的时候控制View的大小、位置、对齐等等。。以FrameLayout为例,看下它的generateLayoutParams(attrs)方法。
public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new FrameLayout.LayoutParams(getContext(), attrs);        
    }
 
很简单,构造了一个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的三条线
onMeasure 测量View的大小
onLayout 对View的布局进行控制
draw绘制该View,drawChild绘制子VIew
分享到:
评论
1 楼 black_smart 2013-03-14  
   

相关推荐

    Android 百分比布局

    百分比布局<android.support.percent.PercentLinearLayout android:id="@+id/tv_solid_number_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft=...

    ANDROID实验报告组件布局.pdf

    Android 开发 (实验五) 实验题目:Android 组件布局试验 指导老师: 班 级:计算机科学与技术系班 姓 名: 一、实验目的 1、掌握 Android 组件布局的使用方法 2、学会组件布局的重要属性与应用 3、能够根据需求,...

    Android控件大全以及各布局空间的使用方式

    layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等) layout_marginLeft - 设置当前元素相对于其容器的左侧...

    Android布局管理器

    5 android:layout_height="fill_parent" 6 android:id="@+id/lla" 7 android:gravity="right" 8 > <!-- 声明一个 LinearLayout 布局,并设置其属性 --> 9 10 android:text="@string/add" 11 android:id="@+id/...

    android自定义百分比布局

    android:layout_height="200dip" android:background="@color/backColor"> </com.hao.percentlayout.layout.PercentLinearLayout> //相对布局 <com.hao.percentlayout.layout.PercentRelativeLayout android...

    Get清风android实验2界面设计:基本组件.doc

    -- 在主布局添加文本框和密码框 --> <TextView android:text = "@string/password" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:...

    android-autofittextview-master.zip_android_android textview_auto

    布局文件: <me.grantland.widget.AutofitLayout android:layout_width= match_parent android:layout_height= wrap_content > <Button android:layout_width= match_parent android:layout_...

    android实验2界面设计:基本组件.doc

    -- 在主布局添加文本框和密码框 --> <TextView android:text = "@string/password" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/password" android:...

    android基础

    第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的嵌套,注意单标签、双标签 拨打电话的实现: 布局activity_main.xml: <LinearLayout xmlns:android=...

    android源码-漂亮的qq界面

    android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="13.0dip" android:layout_marginTop="2.0dip" android:layout_centerVertical="true" /> ...

    深入理解Android中的xmlns:tools属性

    前言 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml...android:layout_height=wrap_content android:textAppearance=@style/Te

    Android实验指导.doc

    利用布局安排各种控件,设计良好用户界面 "<LinearLayout " "xmlns:android="schemas.android./apk/res/android" " "android:orientation="vertical" " "android:layout_width="fill_parent" " "android:layout_...

    WebViewDownloadTest.zip

    废话不多少了,先上布局,布局文件非常简单,一个EditText和一个Button被填充在LinearLayout中其余部分全部留给WebView ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=...

    未加载出真正数据时的显示界面LoaderViewLibrary.zip

     android:layout_height="wrap_content" />在布局文件中定义ImageView的加载页面  android:layout_width="100dp"  android:layout_height="100dp" />用width_weight定义TextView的宽度百分比用于显示加载动画 ...

    android顶部滑动导航

    android:layout_height="match_parent" android:background="#F0EFF5" > android:id="@+id/sayit_rl_tab" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_...

    Android 使用mediaplayer播放res/raw文件夹中的音乐的实例

    (2)修改layout目录下的xml布局文件,添加3个按钮空间和一个文本控件,用于提示当前播放状态和 播放暂停 停止等功能。具体代码如下 <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android ...

    Android移动开发实验4.doc

    " "(2)修改新建项目的res/layout目录下的布局文件activity_main.xml,将" "默认添加的布局代码修改为垂直线性布局管理器,并且删除上、下、左、右" "内边距的设置代码,然后将默认添加的文本框组件删除;...

    CircleImageView

    安卓自定义控件CircleImageView,用它可以实现一个圆形的图片展示,与普通ImageView使用近乎一样,仅需在布局中加上几个特有属性即可。如: xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+...

    Android实验指导(1).doc

    Android实验指导 实验一:系统安装与HelloWorld 【目的】 安装智能手机开发相关软件平台。 【要求】 1. 完成智能手机开发平台安装、以及相关配置 2. 并实现Hello World 3. 了解项目的基本文件目录结构 【原理】 ...

    android 音乐播放器

    //读取布局文件 this.listview = (ListView) super.findViewById(R.id.list);//获取组件 this.listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,this.date));//设置...

Global site tag (gtag.js) - Google Analytics