Android布局设计中的layout_weight的学习
来源:互联网 发布:神雕侠侣 gotv 源码 编辑:程序博客网 时间:2024/06/05 04:18
在Android XML文件的布局设计中,我们经常会遇到Android:layout_weight这个属性。从英文上的意思可以了解到,“weight”是指“分量、重要性”,即该属性指明了widget在设计布局中的重要性。那究竟layout_weight究竟是做什么用的呢?
在线性布局中(LinearLayout)中(默认android:orientation="horizontal"),我们可以使用layout_weight为其视图(View)包含的小组件(widget)或者是容器(container)指定填充权值(重要度)。既是我们可以允许视图中的widget按照layout_weight所指定的数值,按比例分配来填充屏幕的空间。如果我们不指定这个属性,则系统默认为0 ,此时它表示按照widgets实际大小来显示;若layout_weight大于0,则表明存放视图组件的容器空间会按每个组件的layout_weight的值来按比例分配。为了更好更直观地说明这个问题,我用之前做计算器的一部分XML代码来进行举例说明:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:background="#F000" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" > <EditText android:id="@+id/etResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:textSize="25sp" android:text="" android:layout_gravity="center" android:gravity="left" android:cursorVisible="true" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginTop="4dp" /> <TableLayout android:id="@+id/tablelayoyt" android:layout_marginTop="4dp" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearlayout02" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btnC" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/delete" android:layout_weight="2"/> <Button android:id="@+id/btnCE" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/deleteall" android:layout_weight="1"/> </LinearLayout> </TableRow> </TableLayout></LinearLayout>
1、 首先应让两个Button的layout_width="fill_parent"
第一个Button的layout_weight设置为“2”,第二个Button的layout_weight设置为“1”,运行结果如下:
从图中我们可以看到,第一个按钮的长度为第二个按钮长度的1/2,既是屏幕长度的1/3,以此类推。这也说明了,如果layout_width="fill_parent"时,layout_weight的值越小,则该widget占用的空间长度比例越高。
2、 让两个Button的layout_width="wrap_content"
第一个Button的layout_weight设置为“2”,第二个Button的layout_weight设置为“1”,运行结果如下:从此图可以看出,它与layout_weight=”fill_parent”完全相反。这说明了,如果layout_weight=”wrap_content”时,layout_weight的值越小,则该widget占用的空间长度比例越低。
3、 两个Button的layout_width属性相等,layout_weight="1"
如下图所示:从图中可以看出,两个Button的长度相同。这说明了,如果设计布局中的所有widget的layout_weight都相同,则它们会按照同样的比例来填充屏幕空间。
- Android布局设计中的layout_weight的学习
- android 布局中的layout_weight的作用
- Android布局学习四:layout_weight 的使用
- android布局中的layout_weight属性
- Android布局的layout_weight属性
- Android 布局学习之——LinearLayout的layout_weight属性
- android中的layout_weight的含义
- android布局---android:layout_weight
- ANDROID布局中LAYOUT_WEIGHT的作用
- Android布局细节:layout_weight的使用方法
- Android布局属性Layout_weight的标准解释
- Android 布局 LinearLayout 的 layout_weight 属性
- Android布局中Layout_weight的属性
- Android布局中Layout_weight的属性
- Android布局中Layout_weight的属性
- Android LinearLayout布局的layout_weight属性探究
- Android布局中Layout_weight的属性
- Android布局之layout_weight的用法
- 如何修改右键菜单
- erlang学习资源
- java中有关覆写的总结
- QT事件过滤器
- 关机的批处理代码
- Android布局设计中的layout_weight的学习
- maven插件
- 我们工作到底为了什么?(这篇文章很重要,强烈推荐)
- SDUTOJ 1525 字符统计2
- Hibertemplate
- 局域网IPC入侵心得
- C语言面向对象的模拟(1)
- JS获取事件源的位置
- Ajax的一个疑问