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

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>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值