zoukankan      html  css  js  c++  java
  • android--LinearLayout的child中layout_weight的作用与使用

    linearLayout中包含有weight的child时,linearLayout会measure两次:

    第一次 测量 child 的 原始值:

    第二次 测量 child 的 比重值:

    然后将2次测量的值相加,得到child 的具体的宽 或 高。

    //-----------------------------------------------------------------------------

    我们假设 一个LinearLayout方向为横向android:orientation="horizontal" ,它有2个child,分别为 child_1 和 child_2。

    我们来分析下 这2个child的宽度在带有layout_weight属性的情况下,值是怎么计算的。

    第一次测量时:有自己的layout_width参数决定

    首先,child的宽度(layout_width)有3种情况:0dp(具体值,可以随意设置,例如 100dp)、wrap_content、match_parent。

    这3种情况,当应用加载到具体的手机上时,都会在测量时,变为具体的值,分别如下:

    1,当child_1 的 layout_width 为 0dp,它的width_orginal 原始宽度为0pix;(100dp,则 width_orginal 为 屏幕密度density * 100 pix = X_1 pix)

      (其中手机屏幕密度 通过 context.getResources().getDisplayMetrics().density 获取)

    2,当child_1 的 layout_width 为 wrap_content,它的width_orginal 原始宽度为 int_wrap_cotent(在具体的手机上是一个固定值,假设为 X_1  pix) ;

    3,当child_1 的 layout_width 为 match_parent,它的width_orginal 原始宽度为手机屏幕的宽度(在具体的手机上是一个固定值,假设为 X_1 pix) ;

    (child_2 的 原始宽度为  X_2)

    第二次测量时:由所有的child 的 layout_weight 参数共同决定

    假设 2个child的 layout_weight 分别为 weight_1 和 weight_2 (layout_weight 参数 是一个int类型值,可以为0):

    那么 child_1 和 child_2 的最终宽度为:

    child_1的宽度:X_1 + (手机屏幕宽度 - (X_1 + X_2))  * weight_1 / (weight_1+weight_2) = 最终宽度。

    以下是几个例子:

    demo1:child_1 和 child_2 的layout_width都为0dp,layout_weight分别为 1 和 2

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/root"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="horizontal" >
     7 
     8     <TextView
     9         android:id="@+id/tv_left"
    10         android:layout_width="0dp"
    11         android:layout_height="wrap_content"
    12         android:layout_weight="1"
    13         android:background="#00ff00"
    15         android:text="child_1"
    16         android:textSize="24sp" />
    17 
    18     <TextView
    19         android:id="@+id/tv_right"
    20         android:layout_width="0dp"
    21         android:layout_height="wrap_content"
    22         android:layout_weight="2"
    23         android:background="#ff0000"
    24 
    25         android:text="child_2"
    26         android:textSize="24sp" />
    27 
    28 </LinearLayout>

    此时,child_1 的最终宽度为:0 pix + 屏幕宽度 * 1/3 = 1/3 的屏幕宽度。child_2 最终宽度为 2/3 的屏幕宽度。

    所以想要按比例分配LinearLayout的children的宽(高)值,可以用次方法。

    demo2:child_1 和 child_2 的layout_width都为100dp,layout_weight分别为 1 和 2。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/root"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="horizontal" >
     7 
     8     <TextView
     9         android:id="@+id/tv_left"
    10         android:layout_width="100dp"
    11         android:layout_height="wrap_content"
    12         android:layout_weight="1"
    13         android:background="#00ff00"
    14 
    15         android:text="child_1"
    16         android:textSize="24sp" />
    17 
    18     <TextView
    19         android:id="@+id/tv_right"
    20         android:layout_width="100dp"
    21         android:layout_height="wrap_content"
    22         android:layout_weight="2"
    23         android:background="#ff0000"
    24 
    25         android:text="child_2"
    26         android:textSize="24sp" />
    27 
    28 </LinearLayout>

    此时,child_1 的最终宽度为:100 * density pix + (屏幕宽度 - 200 dp * density) * 1/3 = 。。。

    demo2:child_1 和 child_2 的layout_width都为match_parent,layout_weight分别为 1 和 2。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/root"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="horizontal" >
     7 
     8     <TextView
     9         android:id="@+id/tv_left"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:layout_weight="1"
    13         android:background="#00ff00"
    14 
    15         android:text="child_1"
    16         android:textSize="24sp" />
    17 
    18     <TextView
    19         android:id="@+id/tv_right"
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:layout_weight="2"
    23         android:background="#ff0000"
    24 
    25         android:text="child_2"
    26         android:textSize="24sp" />
    27 
    28 </LinearLayout>

    此时,child_1 的最终宽度为:屏幕宽度 + (屏幕宽度 - 2倍的屏幕宽度) * 1/3 = 2/3 的 屏幕宽度,child_2为 1/3 的屏幕宽度。

    当 child 的 layout_width 都为 match_parent时,想要 child_1 : child_2 = 1 : 2(即 1/3 : 2/3),可以设置 child_1 child_2 的 layout_weight 为 (分母 - 1) : (分母 - 2).

    当 child 的 layout_width 都为 match_parent时,想要child_1 : child_2 : child_3 = 3 : 4 : 5 (即 3/12 : 4/12: 5/12),可以设置layout_weight 分别为 (12-3):(12-4):(12-5)

  • 相关阅读:
    【ABAP系列】SAP 系统的消息类型分析 MESSAGE TYPE
    【FICO系列】SAP FICO-模块 关于固定资产年结和折旧的问题
    【ABAP系列】SAP ABAP 刷新SCREEN的方法
    【ABAP系列】SAP ABAP 控制ALV单元格编辑后获取新的数值
    【ABAP系列】SAP ABAP系统变量及注释
    【EWM系列】SAP EWM凭证对象表概览
    【EWM系列】SAP EWM中仓库任务WT创建的函数
    教你快速录制gif动图
    阿里巴巴的26款超神Java开源项目!
    HTTP和HTTPS协议,看一篇就够了
  • 原文地址:https://www.cnblogs.com/wangziqiang/p/4185208.html
Copyright © 2011-2022 走看看