Android-02-布局管理器-相对布局RelativeLayout

相对布局(RelativeLayout)

1.相对布局管理器,是需要有一个参考对象来进行布局的管理器,首先要有一个参考的组件,例如父类桌面的顶部,左部,右部,底部,或者相对于同级元素的任意位置
2.相对布局非常强大,它可以消除嵌套试图组,并使布局层次结构保持扁平化,从而提高性能,多层嵌套的LinearLayout可以用一个LinearLayout替换

1.相对布局基本语法格式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
属性列表
<LinearLayout >

起始标签后边是固定格式为XML命名空间属性

2.RelativeLayout的两个重要属性

  • gravity----设置布局中的各个控件的布局方式
  • ignireGravity----设置了该属性为ture的属性组件,将不受Gravity属性的影响

RelativeLayout提供了一个内部类,RelativeLayout .LayoutParams,通过这个内部类可以更好的控制界面中的各个控件,支持常用XML属性

1.以布局管理器(父容器)定位

在这里插入图片描述

  • layout_alignParentLeft----layout_alignStart----左对齐
  • layout_alignParentRight----layout_alignEnd----右对齐
  • layout_alignParentTop----顶部对齐
  • layout_alignParentBottom----底部对齐
  • layout_centerHorizontal----水平居中
  • layout_centerVertical----垂直居中
  • layout_centerInParent----中间位置—取值:true,false

2.根据兄弟组件定位

在这里插入图片描述

  • layout_toLeftOf----layout_toStartOf----参考组件的左边----取值:相对的id名
  • layout_toRightOf----layout_toEndOf----参考组件的右边
  • layout_above----参考组件上边
  • layout_below----参考组件的下方
  • layout_alignTop----对其参考组件的上边界
  • layout_alignBottom-----对其参考组件的下边界
  • layout_alignRight----layout_alignEnd----对其参考组件的右边界
  • layout_alignLeft----layout_alignStart----对其参考组件的左边界

3.外边距margin(偏移)—设置组件与父容器的边距,又叫偏移

  • layout_margin----设置组件上下左右的偏移量
  • layout_marginLeft----layout_marginStart----设置组件离左边的偏移量
  • layout_marginRight----layout_marginEnd----设置组件离右边的偏移量
  • layout_marginTop----设置组件离上边的偏移量
  • layout_marginBottom----设置组件离底边的偏移量
  • layout_marginHorizontal----设置组件离水平的偏移量
  • layout_marginVertical----设置组件离垂直的偏移量

4.内边距(padding)填充----设置组件内部元素间的边距,(例如Textview里的字体位置)

  • android:padding—往内部控件部的上下左右填充一定边距
  • paddingLeft----paddingStart----往内部控件的左填充一定边距
  • paddingRight----paddingEndaa往内部控件的右部填充一定边距
  • paddingTop----往内部控件的上部填充一定边距
  • paddingBottom----往内部控件的底部填充一定边距
  • paddingHorizontal----水平填充
  • paddingVertical----垂直填充

实例1

<!-- 在容器的中央 -->
    <Button 
        android:id="@+id/bt1"                             `设置id`
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"               `取值:true,false`
        android:text="firstButton"
        android:textColor="#ff0000"
        />

在这里插入图片描述

<!-- 在容器的中央 -->
    <Button 
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="firstButton"
        android:textColor="#ff0000"
        />
    
<!-- 在firstButton左边 -->
    <Button 
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"           `保持同一水平线,默认去顶部`
        android:layout_toStartOf="@id/bt1"				`相对的控件的id`
        android:text="secondButton"
        android:textColor="#00ff00"
        />

在这里插入图片描述

实例2

        <TextView 
            android:id="@+id/text_v1"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:text="hhh"
            android:background="#0000ff"
            android:layout_centerHorizontal="true"   		`相对父类水平居中`  
            />
        <LinearLayout 										`嵌套线性布局`
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"					`线性布局里遵从水平`
            android:layout_below="@id/text_v1"
            android:layout_centerHorizontal="true" 				`线性布局里遵从父容器居中`
            >
            
         <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bt1"
            
            />
         
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="bt2" 
             />
            
        </LinearLayout>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值