<?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" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="#60c" />
</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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="#60c" />
</LinearLayout>
android:layout_margin
vs android:padding
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/hello"
android:background="#60c" />
</LinearLayout>
android:layout_gravity
vs android:gravity
Huh? What's the difference?
layout
prefix applies to the parent.
layout_margin
vs padding
<?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" >
<TextView
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/hello"
android:background="#60c" />
</LinearLayout>
Both are children of TextView
<?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="wrap_content" android:layout_height="wrap_content" android:text="EditText" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </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="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</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" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="right" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" /> </LinearLayout> </LinearLayout>
Specify view positions relative to each other.
layout_above
layout_below
layout_toLeftOf
layout_toRightOf
<?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:layout_width="match_parent" android:layout_height="wrap_content" /> </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:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> </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/greeting" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_below="@id/greeting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> </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:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</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/greeting" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/cancel_button" android:layout_below="@id/greeting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> <Button android:layout_toRightOf="@id/cancel_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" /> </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/greeting"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/cancel_button"
android:layout_below="@id/greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:layout_below="@id/greeting"
android:layout_toRightOf="@id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok" />
</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/greeting" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/ok_button" android:layout_below="@id/greeting" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" /> <Button android:layout_below="@id/greeting" android:layout_toLeftOf="@id/ok_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> </RelativeLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:text="Hello" android:background="#360" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:text="World" android:background="#60c" /> </FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="right" android:text="Hello" android:background="#360" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:gravity="center_horizontal|bottom" android:text="World" android:background="#60c" /> </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" >
<View
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:background="#f00" />
<View
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#f00" />
</FrameLayout>
<?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" >
<View
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:background="#f00" />
<View
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#f00" />
</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" android:padding="50dp" > <View android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:background="#60c" /> <View android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#60c" /> <View android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#60c" /> </RelativeLayout>
<?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" android:padding="50dp" android:background="#000" > <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#60c" /> <View android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top" android:layout_marginLeft="50dp" android:layout_marginTop="50dp" android:layout_marginBottom="50dp" android:background="#000" /> </FrameLayout>