Android常用布局
LinearLayout 线性布局
1)、线性布局:用来控制其子View(子视图)以水平或垂直方式展开显示
2)、重要属性:
1)、orientation(方向) : "vertical" 为垂直 "horizontal"为水平
2)、layout_weight(权重)
LinearLayout 应用实例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Message"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send"/>
</LinearLayout>
</LinearLayout>
效果图·1
RelativeLayout 相对布局
1)、相对布局:用来控制其子View(子视图)以相对定位的方法进行布局显示
2)、相对布局时最灵活,最强大,也是学习难度最大的布局
3)、相对布局相关属性比较多:
1)、兄弟视图之间:同方向对齐,反方向对齐
1)、同方向对齐属性:
1)、android:layout_alignLeft ---> 对齐左边界
2)、android:layout_alignTop ---> 对齐上边界
3)、android:layout_alignRight ---> 对齐右边界
4)、android:layout_alignBottom ---> 对齐下边界
2、反方向对齐属性:
1)、android:layout_toLeftOf ---> 当前视图在指定视图的左边
2)、android:layout_toRightOf ---> 当前视图在指定视图的右边
3)、android:layout_above --->当前视图在指定视图的上方
4)、android:layout_below ---> 当前视图在指定视图的下方
2)、与父视图之间:同方向对齐,居中
左对齐:android:layout_alighParentLeft
1)、右对齐:android:layout_alighParentRight
2)、顶端对齐:android:layout_alighParentTop
3)、底部对齐:android:layout_alighParentBottom
4)、水平居中:android:layout_centerHorizontal
5)、垂直居中:android:layout_centerVertical
6)、中央位置:android:layout_centerInParent
RelativeLayout 应用实例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 编辑框 -->
<EditText
android:id="@+id/et_relative_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Message"/>
<Button
android:id="@+id/btn_relative_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="@+id/et_relative_msg"
android:layout_alignRight="@+id/et_relative_msg"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_alignTop="@+id/btn_relative_ok"
android:layout_toLeftOf="@+id/btn_relative_ok"
android:layout_marginRight="30dp"/>
</RelativeLayout>
效果图.1
FrameLayout 帧布局
1)、帧布局中的每一个子VIew(子视图)都代表一个画面,默认以屏幕左上角作为(0,0)坐标,按定义的先后顺序逐次逐屏显示,
后面出现的画面会覆盖前面的画面。
2)、可以通过android:Layout——gravity等属性来指定子视图的位置
3)、gravity : 控制的是当前视图的内容/子视图
4)、layout_gravity :控制的是当前视图自己
FrameLayout 实例
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="280dp"
android:layout_height="280dp"
android:background="#33ffff"
android:layout_gravity="center"/>
<TextView
android:layout_width="240dp"
android:layout_height="240dp"
android:background="#33ccff"
android:layout_gravity="center"/>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#3300ff"
android:layout_gravity="center"/>
<TextView
android:layout_width="160dp"
android:layout_height="160dp"
android:background="#3333ff"
android:layout_gravity="center"/>
<TextView
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#3366ff"
android:layout_gravity="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#3399ff"
android:layout_gravity="center"/>
</FrameLayout>
效果图.1