都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中.

  下面说说它的几种用法(以下例子全为横排 注意android:layout_width值和android:layout_weight值的变化)  

  第一种, 最普遍的-----均分, weight的值越大, 占的空间越大.注意android:layout_width的值都为0dp

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5.  
  6. <FrameLayout
  7. android:layout_width="0dp"
  8. android:layout_height="match_parent"
  9. android:layout_weight="1"
  10. android:background="#000" />
  11.  
  12. <Button
  13. android:layout_width="0dp"
  14. android:layout_height="match_parent"
  15. android:layout_weight="1"/>
  16.  
  17. <FrameLayout
  18. android:layout_width="0dp"
  19. android:layout_height="match_parent"
  20. android:layout_weight="1"
  21. android:background="#888" />
  22.  
  23. </LinearLayout>

   第二种, 占满剩余空间, 不管是否处在最后一个

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5.  
  6. <FrameLayout
  7. android:layout_width="40dp"
  8. android:layout_height="match_parent"
  9. android:background="#000" />
  10.  
  11. <Button
  12. android:layout_width="0dp"
  13. android:layout_height="match_parent"
  14. android:layout_weight="1"/>
  15.  
  16. <FrameLayout
  17. android:layout_width="40dp"
  18. android:layout_height="match_parent"
  19. android:background="#888" />
  20.  
  21. </LinearLayout>

  第三种, 值越大占的空间越小, 值为同布局下其它控件的weight之和时,即消失.注意android:layout_width的值都为match_parent为0时占满. 有什么用途? 呵呵, 存在即是合理的.

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4.  
  5. <FrameLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:layout_weight="1"
  9. android:background="#000" />
  10.  
  11. <Button
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_weight="1.5"/>
  15.  
  16. <FrameLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:layout_weight="1"
  20. android:background="#000" />
  21.  
  22. </LinearLayout>

  最后一种(我会用的), 结合android:weightSum使用, 那这儿就用一个问题引入, 如何将控件放在屏幕的中央, 宽度为屏幕的一半?  嗯.....想啊.....想啊...RelativeLayout? 哦, 没法控制? 哦,只能代码了... 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:gravity="center"
  5. android:weightSum="2">
  6.  
  7. <Button
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="1"/>
  11.  
  12. </LinearLayout>

ok, so easy!

android:layout_weight总有你不知道的用法.的更多相关文章

  1. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  2. Android layout_weight的用法

    android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...

  3. Android Drawable 那些不为人知的高效用法

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...

  4. android:layout_weight的真实含义(转)

    首先声明只有在Linearlayout中,该属性才有效.之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...

  5. Android开发(二十七)——android:layout_weight的真实含义

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

  6. android:layout_weight的真实含义

    首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议, 是因为在设置该属性的同时,设置android:layout_width为wrap_ ...

  7. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  8. Android LinearLayout的android:layout_weight属性

    本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根 ...

  9. Android学习之Layoutinflater的用法

    •她的第一次 话说,那是一个风雪交加的夜晚,看着她独自一个人走在漆黑的小道上,我抓紧跟了过去: 那晚,我们...... 记得第一次接触这个 Layoutinflater 应该是在学习 ListView ...

随机推荐

  1. 【LeetCode 173】Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  2. 从python run 和python unittest两种eclipse运行方式深入理解if __name__ == "__main__"

    在写一个简单的python测试程序的时候,发现eclipse中Run as "Python run 和 Python unittest”结果不一样?为什么会不一样? 先贴一下代码段: # - ...

  3. 用Vmware安装centos5

    Vmware安装过程就不详述了,这里从创建虚拟机开始记录. 选择创建虚拟机 下一步 选择稍后安装 选择安装的操作系统版本,需要说明的是,CentOs 5 就是RHEL 5 设置虚拟机名称及虚拟机位置 ...

  4. Spark学习体会

    在去年图计算工作中,和公司里实习的博士生尝试过Spark后,发现Spark比Hadoop在计算速度上后很大的提高.Spark的计算使用Scala语言编写代码,其中图计算用到了GraphX.对Spark ...

  5. python os.walk()和os.path.walk()

    一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...

  6. bzoj 2132 圈地计划(黑白染色,最小割)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2132 [题意] 给定n*m个区域,建工业区价值A,建商业区价值B,如果(i,j)有k个 ...

  7. 一个鼠标键盘控制两台甚至多台主机的方法--Synergy

    在多台主机,不同系统中操作.避免了更换键鼠的麻烦.即使下面图中的功能. 鼠标同时在三台或者多台主机之间进行移动,而且是无缝滑动,鼠标直接从左滑倒右,而且支持,这台电脑复制,另一台黏贴.非常的方便实用. ...

  8. HDU5071 - Chat(STL模拟)

    题目描述 略... 题解 现场赛的时候真是脑残...用splay去写..写完发现调试不出来...然后才发现数据范围才5000...不过那时候只有40分钟了..用数组模拟了速度敲了一发.写完只剩10几分 ...

  9. Protocol Buffers编码详解,例子,图解

    Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...

  10. 安卓app开发方式之webApp

    1.phonegap: 专注于webapp调用native的功能.2.ionic: 专注于webapp的前端ui技术,需要与phonegap(准确的说是和Cordova配合使用).ionic是一个专注 ...