本文主要介绍Android LinearLayout的android:layout_weight属性意义

android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。

除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下

比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,

则比例为20%:50%:30%=2:5:3。layout代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <TextView android:id="@+id/textView1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="match_parent"
  9. android:layout_weight="2"
  10. android:layout_alignParentLeft="true"
  11. android:gravity="center_vertical"
  12. android:text="文本1" />
  13. <TextView android:id="@+id/textView2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="match_parent"
  16. android:layout_weight="5"
  17. android:layout_alignParentLeft="true"
  18. android:gravity="center_vertical"
  19. android:text="文本2" />
  20. <TextView android:id="@+id/textView3"
  21. android:layout_width="wrap_content"
  22. android:layout_height="match_parent"
  23. android:layout_weight="3"
  24. android:layout_alignParentLeft="true"
  25. android:gravity="center_vertical"
  26. android:text="文本3" />
  27. </LinearLayout>

从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可

其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置

最新文章

  1. UML入门
  2. maven 打包
  3. mac ssh localhost
  4. WINDOWS下绑定ARP绑定网关
  5. C语言之贪吃蛇
  6. [设计模式] 7 桥接模式 bridge
  7. HDU 5818 Joint Stacks
  8. c# 请问如何将四个RadioButton分成两组?
  9. C# DateTime.Now 用法小记
  10. lucene 索引查看工具
  11. linux安装Eclipse c++环境
  12. 201521123080《Java程序设计》第4周学习总结
  13. 七牛php-sdk使用-文件上传
  14. jquery/js知识点收藏
  15. 电子产品使用感受之——为什么我把Apple Watch S2 升级到了 S4?
  16. vmware虚拟机安装vmware tools
  17. How to blog on Github
  18. Git的上传步骤
  19. 在webpack自定义配置antd的按需加载和修改主题色
  20. 模板(Template)系统

热门文章

  1. Java 线程内异常处理
  2. 7 个 Bootstrap 在线编辑器用于快速开发响应式网站
  3. 实现iframe窗口高度自适应的又一个巧妙思路
  4. 理解闭包的微观世界和JS垃圾回收机制
  5. Linq-语句之存储过程
  6. Mongo的安全验证
  7. Jmeter-Maven-Plugin高级应用:Log Levels
  8. ArcGIS中的批量处理
  9. OC 创建单例
  10. ONVIFclient搜索设备获取rtsp地址开发笔记(精华篇)