布局管理器
LinearLayout(线性布局)
RelativeLayout(相对布局)
2-1-1 LinearLayout
常用属性
android:id 查找控件的唯一标识
android:layout_width 宽度
android:layout_height 高度
android:background 背景
android:layout_margin 外边距
android:layout_padding 内边距
android:orientation 方向(默认水平方向,线性布局特有)
使用:
<LinearLayout
android:id="@+id/ll_1"
android:layout_width=""
android:layout_height=""
android:orientation="">
</LinearLayout>
其中:
- layout_width、layout_height属性值分为:
wrap_content 包含内容,内容多少宽度即为多少;
match_parent 匹配父控件 - orientation属性值为:
horizontal 水平方向
vertical 垂直方向
注意:
android 中的宽度高度单位为 dp e.g. 100dp
android 中的字体单位为 sp e.g. 10sp
这是由于android 屏幕碎片化较严重,有各种机型、分辨率等。
dp、sp 可根据分辨率自己适配、计算出单位来。
<View
android:id="@+id/v_1"
android:layout_width="0dp"
android:layout_weidht="1"/>
<View
android:id="@+id/v_2"
android:layout_width="0dp"
android:layout_weight="1"/>
- 是所有控件的父类,TextView、Button等最终都继承自 View,
View 的父控件是 LinearLayout - gravity 内部元素排放的对其方式
- layout_weight 权重,将剩余内容按照权重进行分配
上述代码中设置的两个View中layout_weight均为“0dp”,权重均为“1”,所以以上两个View将剩余内容平分,各占一半。
2-1-2 RelativeLayout
最常用属性:
android:layout_toLeftOf 在…的左边
android:layout_toRightOf 在…的右边
android:layout_alignButton 与…的底部对齐
android:layout_alignParentBottom 与父控件底部对齐
android:layout_brlow 在…的下边
使用:
android:layout_below="@+id/view_1" 在view_1的下面
android:layout_toRightOf="@+id/view_1" 在view_1的右边
其他用法与LinearLayout(线性布局)基本相同