分别是LinearLayout , RelativeLayout, 和FrameLayout
linear layout有水平和竖直之分可以嵌套
三种常用的属性关系
android:gravity
- Controls the alignment of the view content (akin to text-align in CSS)android:layout_gravity
- Controls the alignment of the view within it's parent container (akin to float in CSS)android:layout_weight
- Specifies how much of the extra space in the layout to be allocated to a view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Email"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"/>
<LinearLayout
android:weightSum="5"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:src="@android:drawable/ic_menu_add"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"/>
<ImageButton
android:src="@android:drawable/ic_input_add"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
</LinearLayout>
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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1245678901234567890"
android:id="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL IT"
android:id="@+id/button2"
android:layout_below="@id/button1"
android:layout_alignLeft="@id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button2"
android:height="200dp"
android:text="1245678901234567890"
android:id="@+id/button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button3"
android:text="CANCEL IT"
android:id="@+id/button4"
android:layout_toRightOf="@id/button3" />
</RelativeLayout>
FrameLayout Z-order布局形式,后面的元素会覆盖前面的元素。
In a frame layout, the children are displayed with a z-index in the order of how they appear. Put simply, the last child added to a FrameLayout
will be drawn on top of all the previous children.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Child1 is drawn first -->
<ImageView
android:id="@+id/child1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="Image"
android:src="@android:drawable/ic_menu_camera" />
<!-- Child2 is drawn over Child1 -->
<TextView
android:id="@+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Child 2"
android:layout_gravity="top|left" />
<!-- Child3 is drawn over Child1 and Child2 -->
<TextView
android:id="@+id/child3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Child 3"
android:layout_gravity="top|right" />
</FrameLayout>