• Android零基础入门第28节:轻松掌握RelativeLayout相对布局


    在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。

     

    一、认识RelativeLayout

    RelativeLayout,又叫相对布局,使用RelativeLayout标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。

    下表显示了RelativeLayout支持的常用XML属性及相关方法的说明。

    为了控制该布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类: RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。

    在相对于容器定位的属性主要有以下几个,属性值为true或false。

    • android:layout_centerHorizontal:控制该组件是否和布局容器的水平居中。

    • android:layout_centerVertical:控制该组件是否和布局容器的垂直居中。

    • android:layout_centerInparent:控制该组件是否和布局容器的中央位置。

    • android:layout_alignParentTop:控制该组件是否和布局容器的顶部对齐。

    • android:layout_alignParentBottom:控制该组件是否和布局容器的底端对齐。

    • android:layout_alignParentLeft:控制该组件是否和布局容器的左边对齐。

    • android:layout_alignParentRight:控制该组件是否和布局容器的右边对齐。

    • android:layout_alignParentStart:控制该组件是否和布局容器的开始对齐。

    • android:layout_alignParentEnd:控制该组件是否和布局容器的末端对齐。

    • android:layout_alignWithParentIfMissing:如果对应的兄弟组件找不到的话就以父容器做参照物。

    在相对于其他组件定位的属性主要有以下几个,属性值为其他组件的id。

    • android:layout_toLeftOf:本组件在某组件的左边。

    • android:layout_toRightOf:本组件在某组件的右边。

    • android:layout_toStartOf:本组件在某组件开始端。

    • android:layout_toEndOf:本组件在某组件末端。

    • android:layout_above:本组件在某组件的上方。

    • android:layout_below:本组件在某组件的下方。

    • android:layout_alignBaseline:本组件和某组件的基线对齐。

    • android:layout_alignTop:本组件的顶部和某组件的的顶部对齐。

    • android:layout_alignBottom:本组件的下边缘和某组件的的下边缘对齐。

    • android:layout_alignRight:本组件的右边缘和某组件的的右边缘对齐。

    • android:layout_alignLeft:本组件左边缘和某组件左边缘对齐。

    • android:layout_alignStart:本组件的开始端和某组件开始端对齐。

    • android:layout_alignEnd:本组件的末端和某组件末端对齐。

    除此之外,RelativeLayout.LayoutParams 还继承了 android view. ViewGroup.MarginLayoutParams,因此 RelativeLayout 布局容器中每个子组件也可指定 android.view.ViewGroiip.MarginLayoutParams所支持的各XML属性。

     

    二、示例

    接下来通过一个简单的示例程序来学习RelativeLayout的使用用法。

    继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:

    <?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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器左上侧"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>
        <!-- 定义该组件位于父容器上侧水平居中 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器上侧水平居中"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>
        <!-- 定义该组件位于父容器右上侧 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器右上侧"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"/>
        <!-- 定义该组件位于父容器左侧垂直居中 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左中"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <!-- 定义该组件位于父容器右侧垂直居中 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="右中"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
        <!-- 定义该组件位于父容器左下侧 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器左下侧"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"/>
        <!-- 定义该组件位于父容器下侧水平居中 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器下侧水平居中"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>
        <!-- 定义该组件位于父容器右下侧 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器右下侧"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"/>
    
    
        <!-- 定义该组件位于父容器中间 -->
        <Button
            android:id="@+id/center_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="容器中央"
            android:layout_centerInParent="true"/>
        <!-- 定义该组件位于center_btn组件的上方 -->
        <Button
            android:id="@+id/center_top_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中上"
            android:layout_above="@id/center_btn"
            android:layout_alignLeft="@id/center_btn"/>
        <!-- 定义该组件位于center_btn组件的下方 -->
        <Button
            android:id="@+id/center_bottom_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中下"
            android:layout_below="@id/center_btn"
            android:layout_alignLeft="@id/center_btn"/>
        <!-- 定义该组件位于center_btn组件的左边 -->
        <Button
            android:id="@+id/center_bottom_left_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中下左"
            android:layout_toLeftOf="@id/center_btn"
            android:layout_alignTop="@id/center_bottom_btn"/>
        <!-- 定义该组件位于center_btn组件的右边 -->
        <Button
            android:id="@+id/center_top_right_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中上右"
            android:layout_toRightOf="@id/center_btn"
            android:layout_alignTop="@id/center_top_btn"/>
    </RelativeLayout>

    运行程序,可以看到下图所示界面效果:

    到此,RelativeLayout的示例结束,关于RelativeLayout的更多用法可以参照上面的XML属性和方法参照表,建议多动手练习。

    今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

    此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若转载请备注出处,特此声明!

    往期总结分享:

    Android零基础入门第1节:Android的前世今生

    Android零基础入门第2节:Android 系统架构和应用组件那些事

    Android零基础入门第3节:带你一起来聊一聊Android开发环境

    Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招

    Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神

    Android零基础入门第6节:配置优化SDK Manager, 正式约会女神

    Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅

    Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

    Android零基础入门第9节:Android应用实战,不懂代码也可以开发

    Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

    Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

    Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌

    Android零基础入门第13节:Android Studio配置优化,打造开发利器

    Android零基础入门第14节:使用高速Genymotion,跨入火箭时代

    Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航

    Android零基础入门第16节:Android用户界面开发概述

    Android零基础入门第17节:TextView属性和方法大全

    Android零基础入门第18节:EditText的属性和使用方法

    Android零基础入门第19节:Button使用详解

    Android零基础入门第20节:CheckBox和RadioButton使用大全

    Android零基础入门第21节:ToggleButton和Switch使用大全

    Android零基础入门第22节:ImageView的属性和方法大全

    Android零基础入门第23节:ImageButton和ZoomButton使用大全

    Android零基础入门第24节:自定义View简单使用,打造属于你的控件

    Android零基础入门第25节:简单且最常用的LinearLayout线性布局

    Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同

    Android零基础入门第27节:正确使用padding和margin

  • 相关阅读:
    好看的壁纸网站
    python简介
    python学习之基本语法(1)
    信息系统开发方法
    数据库连接池的使用小结
    软件版本后的字母含义
    信息系统与信息化
    软考
    实施过程中的项目管理
    mysql查SQL执行速度
  • 原文地址:https://www.cnblogs.com/cqkxzsxy/p/7305957.html
Copyright © 2020-2023  润新知