RelativeLayout初步了解

特点:按组件的相对位置来多布局,如某个组件在某个组件的上下左右。

常用属性:
XML属性 描述
android:layout_above 指定该组件在某个组件上方
android:layout_below 指定该组件在某个组件下方
android:layout_toLeftOf 指定该组件在某个组件左边
android:layout_toRightOf 指定该组件在某个组件的右边
android:layout_centerInParent 指定该组件在父组件的中间
android:layout_centerVertical 垂直居中
android:layout_centralHorizontal 水平居中
android:layout_alignRight 指定这个组件和哪个组件左边对齐
android:layout_alignLet 指定这个组件和哪个组件右边对齐
android:layout_alignBottom 指定这个组件和哪个组件底端对齐
android:layout_alignTop 指定这个组件和哪个组件顶端对齐
android:layout_alignParentRight 用于指定该组件是否与父控件右端对齐
android:layout_alignParentLeft 指定该组件是否与父控件左端对齐
android:layout_alignParentTop 指定该组件是否与父控件顶端对齐
android:layout_alignParentDown 指定该组件是否与父控件下面对齐
代码示意:
  
  
<?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/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中" />
 
<Button
android:id="@+id/btn_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_center"
android:layout_alignLeft="@+id/btn_center"
android:text="上" />
 
<Button
android:id="@+id/btn_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_center"
android:layout_centerHorizontal="true"
android:text="下" />
 
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_center"
android:text="左" />
 
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_center"
android:layout_toRightOf="@+id/btn_center"
android:text="右" />
 
</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" >
 
<Button
android:id="@+id/btn_center1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中" />
 
<Button
android:id="@+id/btn_left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_center1"
android:text="左" />
 
<Button
android:id="@+id/btn_right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_left1"
android:layout_alignParentRight="true"
android:text="右" />
 
<Button
android:id="@+id/btn_left2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_center1"
android:text="左" />
 
<Button
android:id="@+id/btn_right2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_left2"
android:layout_alignParentRight="true"
android:text="右" />
 
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值