`

LinearLayout布局中Layout_weight的深刻理解

 
阅读更多

首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重。很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间。

看下面代码:

<?xml version="1.0" encoding="utf-8"?>     
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:orientation="vertical"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent"     
    >     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="left"     
    android:text="one"/>     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="center"     
    android:layout_weight="1.0"     
    android:text="two"/>     
    <EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="right"     
    android:text="three"/>     
</LinearLayout>     

 

运行结果是:

看上面代码:只有Button2使用了Layout_weight属性,并赋值为了1,而Button1和Button3没有设置Layout_weight这个属性,根据API,可知,他们默认是0

下面我就来讲,Layout_weight这个属性的真正的意思:Android系统先按照你设置的3个Button高度Layout_height值wrap_content,给你分配好他们3个的高度,

然后会把剩下来的屏幕空间全部赋给Button2,因为只有他的权重值是1,这也是为什么Button2占了那么大的一块空间。

有了以上的理解我们就可以对网上关于Layout_weight这个属性更让人费解的效果有一个清晰的认识了。

我们来看这段代码:

 <?xml version="1.0" encoding="UTF-8">   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>   
    <TextView   
        android:background="#cccccc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     <TextView   
        android:background="#ddaacc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="3"   
        android:textColor="@android:color/black"   
        android:layout_weight="3" />   
</LinearLayout> 

 

三个文本框的都是 layout_width=“wrap_content 时,会得到以下效果

按照上面的理解,系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,所以就出现了上面的图像。

而当layout_width=fill_parent时,如果分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现下面的效果:

你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=fill_parent时,weighth值越小权重越大,优先级越高,就好像在背口诀

一样,其实他们并没有真正理解这个问题,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我们来分析:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width

同理第二个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;

第三个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。

这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现下面的效果了:

第三个直接不显示了,为什么呢?一起来按上面方法算一下吧:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width

同理第二个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;

第三个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。

分享到:
评论

相关推荐

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

    如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 &lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...

    Android应用中通过Layout_weight属性用ListView实现表格

    几乎没有用到,我们完全可以用LinearLayout和RelativeLayout来代替TableLayout的使用,自己开发中主要使用LinearLayout,RelativeLayout这两种布局,不过刚开始我还是偏爱于RelativeLayout,因为在RelativeLayout...

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

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

    WebViewDownloadTest.zip

    废话不多少了,先上布局,布局文件非常简单,一个EditText和一个Button被填充在LinearLayout中其余部分全部留给WebView ``` xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_...

    Android布局之LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...

    linearLayout线性布局

    &lt;LinearLayout nandroid:layout_width=“0dp” android:layout_weight u201c1” android:layout_height=“match_parent” android:background&gt; 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总...

    android layout 按比例布局的代码

    为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie

    Android实现图片自动轮换

    import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap;... android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; &lt;/android.support.constraint.ConstraintLayout&gt;

    SwipeListViewTest项目

    -- linearlayout中的布局是每一项后面隐藏的布局 --&gt; &lt;LinearLayout android:id="@+id/back" android:layout_width="match_parent" android:layout_height="80dp" android:background="#eee" android:tag="back" &gt;...

    高仿微信界面

    android:layout_weight="1"&gt; android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /&gt; android:layout_width="match_parent" android:layout_height=...

    三联生活周刊应用布局效果.zip

    这个是三联生活周刊应用布局效果的源码,android自定义viewgroup实现等分格子布局,实现这样的效果:一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

     android:layout_weight="1"  android:layout_width="0dp"  android:layout_height="match_parent"/&gt;    android:onClick="scrollTo3"  android:background="@android:color/holo_green_light"  android:...

    LCRapidDevelop-master

    android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...

    android的layout布局种类

    它提供了控件水平垂直排列的模型,同时可以通过设置子控件的 weight 布局参数控制各个控件在布局中的相对大小。LinearLayout 有两种排列方式:水平排列(horizontal)和垂直排列(vertical)。 在 LinearLayout 中...

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

    xml布局中创建CircleIndicator是配合ViewPager使用的,一般如下布局:  android:layout_width="match_parent"  android:layout_height="match_parent"&gt;    android:id="@ id/viewpager_default"  ...

    Android计算器实例

    一个Android计算器实例,布局文件中用到了Linearlayout、TableLayout、android:layout_weight的嵌套

    【Android Studio代码】线性布局

    用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:&lt;LinearLayout&gt;&lt;/LinearLayout&gt;;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。

    Android-EmotionInputDetector:开发微信,电报等情感输入布局的图书馆

    EmotionInputDetector 一个用于开发情感输入布局的库,例如微信,电报等 下载 ... 将高度可变的视图/布局的layout_height设置为0,并将layout_weight设置为1 2.在活动布局的底部添加自定义的情绪输

    详解Android中weight的使用方法

    android中对weight的学习可以说是必须的,如果UI布局仅仅使用dp与sp等等,会让布局显得极度不灵活,毕竟各个手机屏幕大小不同,更别说是还有ipad之类的了,所以也是同做本人近期做的一个小UI来分享一下weight的使用...

    ChildLayout:处理LinearLayout,两层绘制后,无法绘制的情况

    ChildLayout描述为了解决当我们使用 LinearLayout 布局的时候使用了 weight 属性后,再嵌套LinearLayout,又一次在子 LinearLayout 中使用 weight 属性,会导致实际的 View 无法被成功绘制的解决方案,具体实际的...

Global site tag (gtag.js) - Google Analytics