Android--(10)--详解相对布局(RelativeLayout)

来源:互联网 发布:时尚笔记本电脑淘宝 编辑:程序博客网 时间:2024/06/06 15:02

RelativeLayout特点:组件的位置总是相对于兄弟组件或者父容器来决定。如果A的位置由B决定,必须先定义组件B,再定义组件A;

相对布局中的四种控件位置属性:
(1)与兄弟空间外侧的四个方位对齐
    android:layout_toLeftOf=”@id/text1” 该控件在text1的左边;
    android:layout_toRightOf=”@id/text1” 该控件在text1的右边;
    android:layout_above=”@id/text1” 该控件在text1的上边;
    android:layout_below=”@id/text1” 该控件在text1的下边;
(2)与兄弟控件的四个边缘对齐
    android:layout_alignBottom=”@id/text1” 该控件下边线与指定id控件的下边线对齐
    android:layout_alignTop=”@id/text1” 该控件上边线与指定id控件的上边线对齐
    android:layout_alignLeft=”@id/text1” 该控件左边线与指定id控件的左边线对齐
    android:layout_alignRight=”@id/text1” 该控件右边线与指定id控件的右边线对齐
    android:layout_alignBaseline=”@id/text1” 两控件的文字基线对齐
(3)与父控件的四个边缘对齐
     android:layout_alignParentBottom=”true” 是否相对于父控件的下方;
     android:layout_alignParentTop=”true” 是否相对于父控件的上方;
     android:layout_alignParentLeft=”true” 是否相对于父控件的左方;
     android:layout_alignParentRight=”true” 是否相对于父控件的右方;

(4)与父控件的中央对齐
     android:layout_centerInParent=”true” 子类控件与父类控件即水平又垂直居中;
     android:layout_centerHorizental=”true” 子类控件与父类控件水平居中;
     android:layout_centerVertical=”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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView         android:id="@+id/t1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="用户名:"        />    <EditText        android:id="@+id/e1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/t1"        tools:ignore="TextFields" />    <TextView         android:id="@+id/t2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/e1"        android:text="密码:"        />    <EditText         android:id="@+id/e2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/t2"        />    <Button         android:id="@+id/b1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/e2"    //设按钮位于第二个文本框下方;        android:layout_alignRight="@+id/e2"    //设置按钮与第二个文本框的左边框相对齐;        android:text="登录"               /></RelativeLayout>

//学习记录,仅代替笔记!!!