zoukankan      html  css  js  c++  java
  • Android(java)学习笔记96:layout_weight使用注意事项

    1. android:layout_weight使用说明

    layout_weight是权重的意思,也就是各个控件所占的比重,用在LinearLayout布局中。当我们使用layout_weight的时候,layout_width和layout_height有三种表示方法

    2. android:layout_weight使用之 layout_width为0dp

    此时,layout_weight使用的时候要求:layout_height = "0"  或者  layout_width = "0"

    比如:只有如下这样layout_weight属性才会生效

    (1)android:layout_width = "0dip"

             android:layout_weight = "1"

    (2)android:layout_height = "0dip"

             android:layout_weight = "1"

    下面是一个案例:

    首先看看我们的布局需求,如下:

    如下布局中上下各占1/2,然后这两个一半,分别如下方式分割为1/3

    布局代码如下

     1 <?xml version="1.0"  encoding="utf-8"?>
     2 <LinearLayout xmln:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:oritentation="vertical">
     6     
     7     <LinearLayout
     8          android:layout_width="match_parent"
     9      android:layout_height="0dip"
    10      android:layout_weight="1"
    11          android:oritentation="horizontal">
    12          
    13         <View
    14             android:layout_width="0dip"
    15             android:layout_height="match_parent"
    16             android:background="#ff0000"
    17        android:layout_weight="1"
    18            />
    19         <View
    20             android:layout_width="0dip"
    21             android:layout_height="match_parent"
    22             android:background="#0000ff"
    23        android:layout_weight="1"
    24            />
    25         <View
    26             android:layout_width="0dip"
    27             android:layout_height="match_parent"
    28             android:background="#00ff00"
    29        android:layout_weight="1"
    30            />
    31     </LinearLayout>     
    32     <LinearLayout
    33          android:layout_width="match_parent"
    34       android:layout_height="0dip"
    35       android:layout_weight="1"
    36          android:oritentation="vertical">
    37          
    38          <View
    39             android:layout_width="match_parent"
    40        android:layout_height="0dip"
    41        android:layout_weight="1"
    42             android:background="#ff00ff"
    43            />
    44         <View
    45             android:layout_width="match_parent"
    46        android:layout_height="0dip"
    47        android:layout_weight="1"
    48             android:background="#00ffff"
    49            />
    50         <View
    51             android:layout_width="match_parent"
    52        android:layout_height="0dip"
    53        android:layout_weight="1"
    54             android:background="#ffff00"
    55            />
    56     </LinearLayout>    
    57     
    58 </LinearLayout>    

    3. android:layout_weight使用之 layout_width为wrap_content


    我们可以看出,首先他们是先包含自己的内容,然后在剩下的空间中按照weight来划分,即剩下空间按照1:2来划分

    4. android:layout_weight使用之 layout_width为match_parent

     

    我们可以看出,刚好和第一种layout_width为0dp的布局相反

    4. 以上三种情况到底是怎么回事呢?下面来看一下layout_weight的具体计算方法

    layout_weight的意思:

    如果一个控件申明了这个属性,那么这个控件的宽度(高度)等于它原有的长度(宽度或高度)加上剩余空间所占有的比重

    例如:一个假设屏幕的宽度为L.

    (1)对于第一种情况

    原有长度都等于0,所以它们的长度就是剩余空间所占有的比重。

    第一个button所占有的比重为L*1/(1+2)=1/3L

    第二个button所占有的比重为L*2/(1+2)=2/3L

    (2)对于第三种情况

    原有长度都等于match_parent即等于L,那么剩余空间的长度等于L-(L+L)=-L;

    对于第一个button所占有的比重为:-L*1/(1+2)=-1/3L

    对于第二个button所占有的比重为:-L*2/(1+2)=-2/3L,所以它们的总长度等于原有的长度加上剩余空间所占有的比重

    即L+(-1/3L)= 2/3L 和 L+(-2/3L)= 1/3L

    反过来的2:1就是出现的第三种情况

  • 相关阅读:
    JNI 对象处理 (转)
    快速配置vs2012+opencv
    Jni中C++和Java的参数传递(转)
    版本控制之四:SVN客户端重新设置帐号和密码(转)
    版本控制之三:SVN合并及解决冲突(转)
    版本控制之二:SVN的初步使用(转)
    版本控制之一:SVN服务器搭建与安装(转)
    xml读取一行数据
    方法论:怎样练习一万小时
    一位软件工程师的6年总结(转)
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4729591.html
Copyright © 2011-2022 走看看