`
coolerbaosi
  • 浏览: 727742 次
文章分类
社区版块
存档分类
最新评论

关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题

 
阅读更多

转自:http://hi.baidu.com/wei_chou/item/04b51be1abb1e316595dd853

在网上搜索了很多关于layout_weight的文章,众说纷纭,且都不准确。后来自己动手测试,通过分析计算得出以下结论:

1、如果LinearLayout在其子组件相应排列方向上的大小值(layout_width/height)为wrap_content,则忽略所有子组件的layout_weight,且相应方向上的大小值也替换为wrap_content。例如:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Button01"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button02"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button03"/>
</LinearLayout>

由于LinearLayout的android:orientation="horizontal",子组件水平排列,而android:layout_width="wrap_content",所有将忽略所有子组件的layout_weight,并将android:layout_width值替换为wrap_content。垂直方向同理。
2、layout_weight值表示该组件应该增加或减少的值占剩余空间的比例,没有优先级之说。至于为什么有的组件会不显示,这不是因为优先级的原因。通过以下公式的计算,你会明白。本人愚钝,拟公式如下:
W=W1+L*P;
其中W表示最终宽度或高度,W1表示第一次测量的宽度/高度,L表示剩余空间的值(可能为负数),P表示根据android:layout_weight属性值计算出来的百分比。
将上面代码更改如下(注意android:orientation跟android:layout_width的对应关系),此时子组件的layout_width/height和layout_weight将被应用:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Button03"/>
</LinearLayout>
android对组件进行布局的时候会组件大小进行两次计算:第一次是测量没有应用android:layout_weight时的值,第二次是根据第一次测量值重新计算组件应用android:layout_weight之后的实际值。
设LinearLayout父组件宽度为w,由于三个Button的layout_width值都为wrap_content,为简单起见,设第一次测量三个Button的宽度都为W1=w/5,合起来的总宽度为total_w=3w/5。则剩余空间的大小为L=w-total_w=2w/5,下面进行第二次计算,分别对三个Button的实际大小进行计算:
Button01:
P=3/(3+2+5)=0.3;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.3=0.32w;//增加了0.12w

Button02:
P=2/(3+2+5)=0.2;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.2=0.28w;//增加了0.08w

Button03:
P=5/(3+2+5)=0.5;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.5=0.4w;//增加了0.2w
结果显而易见。
那如果三个Button的layout_width值都为fill_parent会怎样呢?此时
W1=w;
L=w-total_w=w-3*W1=-2w;
Button01:
P=3/(3+2+5)=0.3;
W1=w;
W=W1+L*P=w+(-2w)*0.3=0.4w;//减少了0.6w

Button02:
P=2/(3+2+5)=0.2;
W1=w;
W=W1+L*P=w+(-2w)*0.2=0.6w;//减少了0.6w

Button03:
P=5/(3+2+5)=0.5;
W1=w;
W=W1+L*P=w+(-2w)*0.5=0;//宽度为0,所以不显示
由于Button03的宽度为0,所以这时只能显示Button01和Button02。而不是因为其layout_weight值比较大,优先级低导致的。
到这里是不是突然觉得很简单啊!
如果Button03的layout_weight改为7呢?自己去算吧,W将小于0,因此也不会显示。
还有一种情况,如果设Button02的layout_width="wrap_content",其他都为fill_parent呢?计算方法一样。
垂直方向同理。
另外当android:orientation="horizontal"时,垂直方向如何应用?首先适用本文第一条,否则当LinearLayout的android:layout_height="fill_parent"时,若子组件的android:layout_height="fill_parent",则纵向填满,否则自适应大小。
补充说明之妙用:

3、若不设置LinearLayout的weightSum以及所有子组件的layout_weight值,即两者的值都为0,则将要出界的组件的尺寸将会被调整。规则如下:所有值为
wrap_contentfill_parent的组件,若按其正常尺寸,有部分在父组件边界内而部分在父组件边界外的,则将其尺寸调整到正好容纳在边界之内;完全在边界外部的组件将会隐藏,即宽或高为0;所有值不为wrap_contentfill_parent之外的组件,即有固定尺寸的,如100dp,则无论在边界内外都按固定尺寸显示。
问题:我不想给子组件设置固定的尺寸,也不想出界的组件会隐藏,而是想当我滚动内容的时候scrollTo(x,y)/scrollBy(x,y),出界的部分会显示出来而不是依然隐藏。根据前面的第1、2条,我们知道应用weight可以让组件出界,但却会改变组件的大小。其实有简单的办法,见下一条。
4、若LinearLayout的weightSum和子组件的layout_weight有一个不为0,当依次应用子组件的layout_weight来进行计算组件尺寸的时候(见第2条),当某子组件与其前面的所有子组件的layout_weight值之和正好等于weightSum的时候,则忽略其后所有子组件的layout_weight属性,此时,其后的所有子组件将保持原始的尺寸。

根据本条特性,解决上面的问题很容易了,见代码:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0px"
android:layout_height="0px"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="400dp"
android:text="按钮0"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮2"/>
</LinearLayout>
该布局中,由于View的android:layout_weight正好等于android:weightSum(这里省略了,因为android:weightSum默认为所有子组件的android:layout_weight的值之和),所有的Button都会保持其原始的大小并出界。
读者可以自行测试其他情况,将android:weightSum和各子组件的android:layout_weight设置不同的值,看看效果。
3、4条为后来补充。到这里算是完善了。
补充:更为简单且规范的实现组件出界的方法:

重写onMeasure()方法,调整参数,将测量模式设置为MeasureSpec.UNSPECIFIED
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
widthMeasureSpec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
详见我的另一篇文章,对测量(measure)的详细解说。

分享到:
评论

相关推荐

    androidlayout-marginBottom的值为负数.docx

    为什么有时候像android:layout_marginBottom等变量的赋值为负数? 例如如下代码: &lt;LinearLayout  android:orientation="vertical"  android:id="@id/Widget_2X4_frame"  android:layout_width="fill_parent" ...

    Android实训购物车页面

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

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

    text = "@string/password" android:layout_width="match_parent" android:layout_height="wrap_content"/&gt; &lt;EditText android:id="@+id/password" android:layout_width="200sp" android:layout_height="wrap_...

    Android购物车代码

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

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

    编写布局代码,如下 &lt;LinearLayout xmlns:android = android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"&gt; &lt;!-- 主布局中添加文本框和输入框 --&gt; ...

    WeChatSample

    android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wechat.wechat.MainActivity" android:orientation="vertical"&gt; &lt;include layout="@layout/top_...

    android mars视频代码 Layout Layout _01源码 LinearLayout

    www.mars-droid.com/Android开发视频教程 LinearLayout代码 源码 mars老师讲课 android 视频源码 Layout_01(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)

    android_QQ_例子

    用Eclipse加载项目工程 &lt;LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" &gt; android:layout

    Android App中的多个LinearLayout嵌套布局实例解析

    在做android UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般 查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现...

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

    android:layout_weight="1" android:layout_height="wrap_content" android:text="行1列1" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content...

    五子连珠游戏

    android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main" android:orientation="vertical"&gt; &lt;TextView android:layout_width="fill_parent" android:...

    android mars视频代码 Layout Layout _04源码 LinearLayout TableLayout嵌套布局

    www.mars-droid.com/Android开发视频教程 LinearLayout TableLayout代码 源码 mars老师讲课 android 视频源码 Layout_04(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)

    ANDROID实验报告组件布局.pdf

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

    android自定义弹出框

    android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" &gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=...

    android左右滚动焦点图【改】

    android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical"&gt; android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height=...

    Android UI组件LinearLayout线性布局详解

    LinearLayout 线性布局,该布局的继承关系:   1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的。 LinearLayout是线性布局控件,它包含的子控件...– android:layout_width

    android中ratingbar的简单使用

    android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; android:id=...

    android 右侧滑动菜单 主页滑动 窗帘效果 拉幕

    android:layout_width="wrap_content" android:layout_height="fill_parent"&gt; &lt;include layout="@layout/main_left" /&gt; &lt;/LinearLayout&gt; android:id="@+id/sliding_menu_view" android:layout_width="fill_...

    android基础

    android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; android:layout_width="match_parent" android:layout_height="wrap_content" android:text...

    九宫格牌翻转游戏demo

    android:layout_height="match_parent" android:background="#000000" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:...

Global site tag (gtag.js) - Google Analytics