zoukankan      html  css  js  c++  java
  • layout_weight相关知识

      之前使用layout_weight都是在layout_width或layout_height为0dp的时候,都没出现什么问题,但是无意间看到了如果设为match_parent会出现不同效果记录一下。

    先看代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="下面两个TextView宽度都是wrap_content"
            android:textSize="16sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="比重1 : 2"
            android:textSize="16sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#33ff0000"
                android:padding="5dp"
                android:text="左边"
                android:textSize="20sp" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#3300ff00"
                android:padding="5dp"
                android:text="右边"
                android:textSize="20sp" />
        </LinearLayout>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="下面两个TextView宽度都是match_parent"
            android:textSize="16sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="比重1 : 2"
            android:textSize="16sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#33ff0000"
                android:padding="5dp"
                android:text="左边"
                android:textSize="20sp" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="#3300ff00"
                android:padding="5dp"
                android:text="右边"
                android:textSize="20sp" />
        </LinearLayout>
    
    </LinearLayout>

     效果图

    分析

      android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

      设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。

  • 相关阅读:
    instanceof 关键词
    类,类中成员变量,类中成员方法,方法中的局部变量,接口,接口中的方法的访问修饰符
    2.Object
    1API简介
    CMC 实例管理
    BW 转换字符空格问题
    BW ON HANA 业务模型关系与数据取数
    进步缓慢
    BO客户端安装更新,重新启动挂起。
    放弃看图,无差别筛选。
  • 原文地址:https://www.cnblogs.com/yangang2013/p/4912326.html
Copyright © 2011-2022 走看看