android:layout_weight 权重

来源:互联网 发布:mac玩魔兽世界怎么样 编辑:程序博客网 时间:2024/06/05 22:54
LinearLayout子控件最重要的属性 android:layout_weight,   值为整数, 默认为0
    1 当父控件LinearLayout中android:orientation="vertical"时
          子控件高度 = 子控件原本高度  +  (父控件LinearLayout高度 - 所有子控件高度之和)  *  子控件高度权重比
    2 当父控件LinearLayout中android:orientation="horizontal", 
           子控件宽度 = 子控件原本宽度  +  (父控件LinearLayout宽度 - 所有子控件宽度之和) *  子控件宽度权重比
          (match_parent或fill_parent都指指父控件高度或宽度)
例1:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#f00"
        android:background="#0f0"
        android:text="上面"
        android:textSize="40sp" 
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#0f0"
        android:background="#00f"
        android:text="中间"
        android:textSize="40sp" 
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#0f0"
        android:background="#f00"
        android:text="下面"
        android:textSize="40sp" 
        android:layout_weight="1"
        />
</LinearLayout>
---->子控件高度 = 子控件原本高度  +  (父控件LinearLayout高度 - 所有子控件高度之和)  *  子控件高度权重比
上面第个子控件TextView的高度 
    = 1屏高 + [ (1屏高 - 3屏高)  *  1/(1+1+1)  ]
    = 1屏高 + [-2屏高*(1/3) ]
    = 1屏高 - 2屏高*(1/3)
    = 1屏高 - 2/3屏高
    = 1/3 屏高

例2:
<?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>  
分析
**使用公式---->子控件高度 = 子控件原本高度  +  (父控件LinearLayout高度 - 所有子控件高度之和)  *  子控件高度权重比   
**(1) one的高度 = one高度 + [1屏高 - (one高度+two高度+three高度) ] * 0/(1+0+0) 
                        = one高度 + 0
                        = one高度
  (2)two的高度   = two高度 +[1屏高 - (one高度+two高度+three高度) ] * 1/(1+0+0) 
                        = one高度 +[1屏高 - (one高度+one高度+one高度) ]  
                        = one高度 +[1屏高 - 3one高度]
                        = 1屏高 - 2one高度
  (3)three的高度 = one高度 + [1屏高 - (one高度+two高度+three高度) ] * 0/(1+0+0) 
                        = one高度 + 0
'                       = one高度 = three高度

例3:
<?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>

分析: 
  **使用公式子控件宽度 = 子控件原本宽度  +  (父控件LinearLayout宽度 - 所有子控件宽度之和) *  子控件宽度权重比 
  ** 当三个android:layout_width="**"都改为android:layout_width="wrap_content"时, 三个TextView的原本宽度一样宽,该宽度下面使用X表示

        第1个TextView宽度 = X + (1屏宽 - 3X) * 1/(1+2+3)
                                           = X + (1屏宽 - 3X) * 1/6
                                           =X- (1/2)X + (1/6)屏宽
                                            = (1/2)X + (1/6)屏宽
        
        第2个TextView宽度 = X+ (1屏宽 - 3X) * 2/(1+2+3)
                                           = X+ (1屏宽 - 3X) * 1/3
                                           = X- X+ (1/3)屏宽
                                            = (1/3)屏宽
        
        第3个TextView宽度 = X+ (1屏宽 - 3X) * 3/(1+2+3)
                                            = X + (1屏宽 - 3X) * 1/2
                                            = X- (3/2)X + (1/2)屏宽
                                            = (1/2)屏宽 - (1/2)X
    当三个android:layout_width="**"都改为android:layout_width="fill_parent"时, 并把原android:layout_weight改为  1, 2, 2
    三个TextView的原本宽度一样宽,该宽度都是一个屏宽, 使用W表示
        
第1个TextView宽度 = W + (W - 3W) * 1/(1+2+2)
                                           = W + (W - 3W) * 1/5
                                           = W - 2W * (1/5)
                                           = W-(2/5)W 
                                           = (3/5)W
        
第2个TextView宽度 =     W + (W - 3W) * 2/(1+2+2)
                                           = W + (W - 3W) * 2/5
                                           = W - 2W * (2/5)
                                           = W-(4/5)W 
                                           = (1/5)W
        
第3个TextView宽度 =     W + (W - 3W) * 2/(1+2+2)
                                           = W + (W - 3W) * 2/5
                                           = W - 2W * (2/5)
                                           = W-(4/5)W 
                                           = (1/5)W

三个android:layout_width="**"都改为android:layout_width="fill_parent", 并把原android:layout_weight改为  1, 2, 3
三个TextView的原本宽度一样宽,该宽度都是一个屏宽, 使用1W表示

第1个TextView宽度        = W + (W - 3W) * 1/(1+2+3)
                                           =W + (W - 3W) * 1/6
                                           =W - 2W * (1/6)
                                           =W-(1/3)W 
                                           = (2/3)W
        
第2个TextView宽度 =     W + (W - 3W) * 2/(1+2+3)
                                           = W + (W - 3W) * 2/6
                                           = W - 2W * (1/3)
                                           = W-(2/3)W 
                                           = (1/3)W
        
第3个TextView宽度 =     W + (W - 3W) * 3/(1+2+3)
                                           = W + (W - 3W) * 1/2
                                           = W - 2W * (1/2)
                                           = W- W 
                                           =0

0 0