当前位置:   article > 正文

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

layout_marginvertical

相对布局(RelativeLayout

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

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

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

起始标签后边是固定格式为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"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

<!-- 在容器的中央 -->
    <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"
        />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

实例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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/270417
推荐阅读
相关标签
  

闽ICP备14008679号