1.概念
1.1 基础知识
- match_parent:匹配父窗口,填充满
- wrap_content:根据内容大小,来填充空间
1.2 线性布局
线性布局是指子控件以水平或垂直方式排列
2.线性布局的属性
2.1 orientation
具体的值分别为:vertical及horizontal
- vertical
垂直方向
- horizontal
水平方向
2.2 layout_weight
默认值为0:即有多大空间则占据多大空间
大于0,将父组件可用的空间进行分割,分两种情况
-
平均分配或者几等分,对于要分配的控件其layout_weight必须相同的,如都为1,以下为两个Button平均分配的情况
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Reset"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Send"/>
-
占据剩余的所有空间
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Message"/>
Tips:
对于垂直方向时:如果使用layout_weight属性,则对于高度是没有意义的,则layout_height按照Android文档应为:0dp
对于水平方向时:如果使用layout_weight属性,则对于宽度是没有意义的,则layout_width应为:0dp
3.实例
layout_width应为:0dp