了解weight属性
来源:互联网 发布:iphone检测软件 编辑:程序博客网 时间:2024/06/06 01:42
记录一下线性布局经常用到的一个属性weight。
经常使用,今天突然又深入了一点,在此记录。
<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" tools:context=".MainActivity" > <!-- 三个TextView的控件并没有对齐,但是对于其中文本第一行是对齐的。这是因为TextView在LinearLayout中会参考父类的基线 --> <!-- LinearLayout中增加属性 android:baselineAligned="false" 可避免这样的问题。 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" > <TextView android:id="@+id/id" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="11111111" /> <TextView android:id="@+id/id2" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2" /> <TextView android:id="@+id/id3" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3" /> </LinearLayout> <TextView android:id="@+id/id0" android:layout_width="wrap_content" android:layout_height="48dp" android:background="#44ff0000" android:gravity="center" android:text="11111111" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" > <!-- width设置成wrap_content对weight设置的比值有影响 --> <!-- 说明LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配 --> <!-- 实际占用空间 = 控件宽度 + 父控件剩余宽度 * 比例 --> <TextView android:id="@+id/id4" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="11111111" /> <TextView android:id="@+id/id5" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="22222" /> <TextView android:id="@+id/id6" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3333" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" > <!-- 假设最外层是480dp 3个控件都是match_parent 剩余尺寸为 480 - 480*3 = -480*2 --> <!-- id7: 480+(-480*2)*(1/6)=480*(2/3) --> <!-- id8: 480+(-480*2)*(2/6)=480*(1/3)+ --> <!-- id9: 480+(-480*2)*(3/6)=0 --> <!-- 结论:实际占用空间 = 控件宽度 + 父控件剩余宽度 * 比例 --> <TextView android:id="@+id/id7" android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="11111111" /> <TextView android:id="@+id/id8" android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="22222" /> <TextView android:id="@+id/id9" android:layout_width="match_parent" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3333" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:orientation="horizontal" > <!-- LinearLayout中设置android:weightSum="2" 实现LinearLayout中只有一个TextView控件使其占用父控件的一半 --> <!-- 有layout_开头的是交给父控件去处理 --> <TextView android:id="@+id/id10" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="11111111" /> </LinearLayout></LinearLayout>
阅读全文
0 0
- 了解weight属性
- LinearLayout 中的 weight 属性
- Android weight属性详解
- weight属性全面解析
- android weight属性
- android layout-weight属性
- android weight 属性正解
- 属性weight汇总
- Android weight属性
- android weight属性
- 线性布局weight属性
- android控件属性Weight
- LinearLayout之weight属性
- Android weight属性详解
- android布局属性之weight
- Android:layout—-weight属性
- Android的weight、lanyout_weight属性
- 浅析LinearLayout的weight属性
- Socket编程
- Python的包管理工具pip的安装与使用
- dup重定向匿名管道父进程子进程的一些坑包含execvp调用ffmepg
- linux启动过程简介
- 在RedHat/CentOS中安装Mercurial(HG)
- 了解weight属性
- 微信文章分享自定义标题,简介和浓缩图
- 办公小技巧:怎么在PDF文件中添加页码
- Swing程序最佳架构设计—以业务对象为中心的MVC模式
- Oracle11g数据库 安装、卸载
- InfiniBand技术和协议架构分析
- 收藏的博客 -- Qt/C++学习
- 料理机器人!有哪些黑科技改变了家庭生活?
- C