zoukankan      html  css  js  c++  java
  • LinearLayout中的layout_weight属性详解

    线性布局中的layout_weight顾名思义即为定义各个组件在整个界面中所占的权重。常用两种实现方法

        1:最为简单的实现方法,当orientation为水平时垂直layout_height=''0dp",同理当orientation为垂直时,将layout_width设定为0dp,此时在一个直接父布局中layout_width中的数值越大这个组件占的界面就越大。示例代码如下:

     

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            xmlns:tools="http://schemas.android.com/tools"  
            android:id="@+id/LinearLayout1"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:orientation="horizontal"     
            >  
              
            <LinearLayout  
                android:layout_width="0dp"  
                android:layout_height="fill_parent"  
                android:background="#ADFF2F"   
                android:layout_weight="1"/>  
             
              
            <LinearLayout  
                android:layout_width="0dp"  
                android:layout_height="fill_parent"  
                android:background="#DA70D6"   
                android:layout_weight="2"/>  
              
        </LinearLayout>  

    运行图:



    下面来讨论另外一种情况,当水平按照权重分割屏幕时,此时将layout_width设置为wrap_content,此时layout_weight的数值仍遵循数值越大占的屏幕越大。


    那么假如layout_width设置为fill_parent呢?

    这里介绍一种计算方法,这里预先设置一个变量暂时定义为queshao(缺少),例如在一个orientation为horizontal的LinearLayout中,有三个组件的layout_width都设定为了fill_parent,此时queshao=1-3 = -2(因为只有一个parent大家都想fill_parent,那么此时就缺少一个parent)

    假如这三个组件的layout_weight依次为1 2 3,那么公式如下:


    第一个组件:1-queshao*(1/(1+2+3))=2/3

    第二个组件:1-queshao*(2/(1+2+3)) = 1/3

    第三个组件:没空了~~

    示例代码及运行图:



        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            xmlns:tools="http://schemas.android.com/tools"  
            android:id="@+id/LinearLayout1"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent" >  
          
            <TextView  
                android:layout_weight="1"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
                android:text="one"   
                android:background="#98FB98"  
             />  
             <TextView  
                android:layout_weight="2"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
                android:text="two"   
                android:background="#FFFF00"  
             />  
             <TextView  
                android:layout_weight="3"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
                android:text="three"   
                android:background="#FF00FF"  
             />  
          
        </LinearLayout>  


    未完待续。。。

  • 相关阅读:
    openresty
    ATS 相关
    pandas
    flask
    ansible
    zipline
    bcolz
    数据分析 --- concat
    Go --- 基础使用
    Go --- 基础介绍
  • 原文地址:https://www.cnblogs.com/emoji/p/4436839.html
Copyright © 2011-2022 走看看