zoukankan      html  css  js  c++  java
  • android:layout_weight越大所占比例越大和越大所占比例越小的两个例子

    摘要: 我的技术博客经常被流氓网站恶意爬取转载。请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版、有效的链接、正确的代码缩进、更好的阅读体验。

    关于android:layout_weight到底是什么含义,在网上“越大所占比例越大”和“越大所占比例越小”两种说法都有。实际上这两种情况也都有。

    例1:0dp

    <LinearLayout android:orientation="vertical" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent">
        <TextView  android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff0000" android:layout_weight="1" android:text="1"/>
        <TextView  android:layout_width="fill_parent" android:layout_height="0dp" android:background="#00ff00" android:layout_weight="2"  android:text="2"/>
    </LinearLayout>

    例2:

    <LinearLayout android:orientation="vertical" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent">
        <TextView  android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ff0000" android:layout_weight="1" android:text="1"/>
        <TextView  android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="2"  android:text="2"/>
    </LinearLayout>

    对两种情况的解释:

    布局首先会给每个元素分配宽度,剩余的空间再分配给每个元素。

    在分配剩余空间阶段,比例按照layout_weight,也就是越大分得越大。

    在情况一里,固定宽度为0,只有第二阶段;情况二里,分配宽度能把所有空间分满,剩余的空间是负数,所以分配的越大,最终空间越小。

    所以如果你要严格地让它按照某种比例,可以试试定为0dp。此时各个元素的比例即为weight。

  • 相关阅读:
    个人作业——顶会热词进程1.3
    个人作业——顶会热词进程1.2
    每周总结(5.9)
    个人作业2——顶会热词进程1.1
    团队项目冲刺第10天
    CodeForces-1178F1 Short Colorful Strip 区间DP
    LOJ-2362 蚯蚓 队列优化
    Educational Codeforces Round 107 (Rated for Div. 2) G.Chips on a Board 倍增优化DP
    LOJ-2123 最短不公共子串 后缀自动机,子序列自动机
    LGR-084 C Reboot from Blue 贪心 DAG上DP最短路
  • 原文地址:https://www.cnblogs.com/hamhog/p/3907146.html
Copyright © 2011-2022 走看看