• Android:Layout_weight的深刻理解(转)


    本文详细介绍了Android布局中Layout_weight的属性,它是剩余空间的权重

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

    1.初步理解

    先看如下代码:

    <?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>   
    View Code

    运行结果是:

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

    Layout_weight这个属性的真正的意思:

    Android系统先按照你设置的3个EditText高度Layout_height值wrap_content,给你分配好他们3个的高度,

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

    现在我们就对Layout_weight属性有了一个初步的认识了,下面我们进一步了解。

    2.进一步了解

    先看下面代码:

     <?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> 
    View Code

    (1)当三个TextView都 layout_width=wrap_content ”且Layout_weight分别为1,2,3时,会得到以下效果:

           (是剩余空间1:2:3)

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

    (2)当三个TextView都layout_width=fill_parent且Layout_weight分别1,2,2时,会得到以下效果:  

                                                   

    你会发现1的权重小,反而分的多了,这是为什么呢???依照上面理解我们来分析:

    系统先给3个textview分配他们所要的宽度fill_parent,就是屏幕的宽度

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

    那么    第一个TextView的实际宽度=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的比列显示了。

    (3)当三个TextView都layout_width=fill_parent且Layout_weight分别1,2,3,会得到以下效果:  

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

    剩余空间=parent_width-3*parent_width=-2个parent_width

    第一个TextView的实际宽度=parent_width +1/6*(-2parent_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的比列显示了。第三个就直接没有空间了。

    转自:http://mobile.51cto.com/abased-375428.htm

  • 相关阅读:
    ppt 制作圆角三角形
    ROS 错误之 [rospack] Error: package 'beginner_tutorials' not found
    ubuntu下安装搜狗输入法以及出现不能输入中文的解决办法
    <crtdbg.h> 的作用
    mybatis配置
    POJO、Bean和JavaBean
    类类型与反射
    Spring层面的事务管理
    java项目常用架构
    java 遍历数组的几种方式
  • 原文地址:https://www.cnblogs.com/android-for-dh/p/4419234.html
Copyright © 2020-2023  润新知