相对布局可以让空间以相对位置的形式出现在布局的任意位置


控件与父布局对齐


<?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:id="@+id/button_1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignParentLeft="true"    android:layout_alignParentTop="true"    android:text="Button 1"    />    <Button        android:id="@+id/button_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:text="Button 2"        />    <Button        android:id="@+id/button_3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="Button 3"        />    <Button        android:id="@+id/button_4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:text="Button 4"        />    <Button        android:id="@+id/button_5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="Button 5"        />RelativeLayout>

五个按钮分别出现在左上,右上,中间和左下,右下



注意到

android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:layout_alignParentBottom="true"

表示按钮分别与父布局的左,上,右,中,下对齐


控件与控件对齐


<?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:id="@+id/button_1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:text="Button 1"    />    <Button        android:id="@+id/button_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/button_1"        android:layout_toLeftOf="@+id/button_1"        android:text="Button 2"        />    <Button        android:id="@+id/button_3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/button_1"        android:layout_toRightOf="@id/button_1"        android:text="Button 3"        />    <Button        android:id="@+id/button_4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button_1"        android:layout_toLeftOf="@id/button_1"        android:text="Button 4"        />    <Button        android:id="@+id/button_5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/button_1"        android:layout_toRightOf="@id/button_1"        android:text="Button 5"        />RelativeLayout>

Button2到4分别位于Button1的左上,右上,左下,右下



注意到

android:layout_centerInParent="true"android:layout_above="@+id/button_1"android:layout_toLeftOf="@+id/button_1"android:layout_toRightOf="@id/button_1"android:layout_below="@id/button_1"

button_1与父布局对齐居中显示,其他button分别与button_1对齐位于上,左,右,下

更多相关文章

  1. Android(安卓)ImageView ScaleType 图解
  2. android 入门xml布局文件
  3. android 入门xml布局文件
  4. 今天开始记录自学android的历程
  5. Android(安卓)Studio Android(安卓)UI控件学习笔记
  6. Android(安卓)选择控件
  7. Android超炫日期日历控件:TimesSquare
  8. 从零开始学android-一行两个按钮居中 布局
  9. 【4.2】Android_Edittext/Button

随机推荐

  1. Android App Bundle (Android新的应用发
  2. 全志A10平板上的ubuntu终极安装版,支持HDM
  3. android触摸事件处理流程
  4. C#、Golang、Python、Java(Android)之间Des
  5. 学习android两年后有感
  6. Android TextView设置个别字体样式
  7. Andy Rubin 说:「市场上」有约 600 万台 A
  8. Android studio下gradle Robolectric单元
  9. 新书内容连载(3):Android SDK中常用命令行工
  10. [书目20101207]Google Android开发入门与