1.图片有多大布局的申请就有多大
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果
2.与父控件的左顶部对齐
android:layout_alignParentTop="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果和上面一致
3.与父控件的左底部对齐
android:layout_alignParentBottom="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果
4.与父控件的右底部对齐
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果
5.与父控件的右顶部对齐
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果
6.控件的右部和父控件的右部对齐
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
<RelativeLayout
android:layout_toRightOf="@id/abc1"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="@drawable/bg2"
></RelativeLayout>
</RelativeLayout>
运行效果
7.控件的底部边缘与给定ID的底部边缘对齐
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc2"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="@drawable/bg2"
></RelativeLayout>
<RelativeLayout
android:id="@+id/abc1"
android:layout_toRightOf="@+id/abc2"
android:layout_alignBottom="@+id/abc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg1"
></RelativeLayout>
</RelativeLayout>
运行效果
8.水平居中:android:layout_centerHorizontal
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg2"
android:layout_centerHorizontal="true"
></RelativeLayout>
</RelativeLayout>
运行效果:
9.垂直居中 android:layout_centerVertical
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg2"
android:layout_centerVertical="true"
></RelativeLayout>
</RelativeLayout>
运行效果:
10.父控件的中央: android:layout_centerInParent="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/abc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg2"
android:layout_centerInParent="true"
></RelativeLayout>
</RelativeLayout>
运行效果
注:输入 android:可以看到各种样式
给新控件添加id的时候:@+id
要使用控件的时候: @id
——@上官可编程