安卓五大布局
一、LinearLayout线性布局
重点在于weight的使用
将宽度或者高度设置为0px
添加android:layout_weight=”1” 注:1为权重比例
<LinearLayout xmls:android="http://schems.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="math_parent" android:orientation="horizontal"> <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮1" /> <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮2" /> <Button android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮3" /> </LinearLayout>
此处每个Button设置的weight为1,所占比例的自己的weight/总共的weight
二、RelativeLayout相对布局
概念:相对于id的布局,如果没有相对于id,则默认的参照物为父窗体
1、参照物为父控件:
1.水平居中属性:
android:layout_centerHorizontal="true"
2.垂直居中属性:
android:layout_centerVertical="true"
3.相对于父控件居中:在父空间内水平和垂直一起居中
android:layout_centerInParent="true"
4.相对于父控件的边缘位置:靠近上边缘/下边缘/左边缘/右边缘:
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
2、参照物为id控件:
1.位于id控件的上方:
android:layout_above="@id/btn1"
2.位于id控件的下方:
android:layout_blow="@id/btn1"
3.位于id控件的左方:
android:layout_toLeftOf="@id/btn1"
4.位于id控件的右方:
android:layout_toRightOf="@id/btn1"
三、FrameLayout帧布局
类似于图层,从上往下依次在实际效果中为从底部往上,例如视频暂停的时候显示在视频上方的暂停图标
<FrameLayout xmls:android="http://schems.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="math_parent">
<TextView
android:layout_width="300px"
android:layout_height="300px"
android:layout_gravity="center"//位于中间,此处待补充,思考:于Layout_centerInParent的区别
/>
</FrameLayout>
四、TableLayout表格布局
TableRow表示第一行,里面的空间从上往下在实际效果中就是从左边第一列开始
<TableLayout xmls:android="http://schems.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="math_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
五、AbsoluteLayout绝对布局
此布局已经被废弃,不推荐使用,多用于指定屏幕的设备,比如为一个屏幕为2k的屏幕定制机顶盒
layout_x="20px"//相对于x坐标的像素点