04 Android基础--RelativeLayout

news2024/9/20 6:06:11

04 Android基础--RelativeLayout

    • 什么是RelativeLayout?
    • RelativeLayout的常见用法:

什么是RelativeLayout?

相对布局(RelativeLayout)是一种根据父容器兄弟控件作为参照来确定控件位置的布局方式。

根据父容器定位
在相对布局中,可以通过以下的属性让的组合让控件处于父容器左上角、右上角、左下角、右下角、上下左右居中,正居中等九个位置。属性如下:

  1. android:layout_alignParentLeft=“true” 父容器左边
  2. android:layout_alignParentRight=“true” 父容器右边
  3. android:layout_alignParentTop=“true” 父容器顶部
  4. android:layout_alignParentBottom=“true” 父容器底部
  5. android:layout_centerHorizontal=“true” 水平方向居中
  6. android:layout_centerVertical=“true” 垂直方向居中
  7. android:layout_centerInParent=“true” 水平垂直都居中

在这里插入图片描述

根据兄弟控件定位
在相对布局中,还支持通过已确定位置的控件作为参考来确定其他控件的位置,以下的属性让的组合让控件处于另外控件左上角、右上角、左下角、右下角、正上方、正下方、正左方、正右方等位置。属性如下:

  1. android:layout_toLeftOf=“@+id/button1” 在button1控件左方
  2. android:layout_toRightOf=“@+id/button1” 在button1控件右方
  3. android:layout_above=“@+id/button1” 在button1控件上方
  4. android:layout_below=“@+id/button1” 在button1控件下方
  5. android:layout_alignLeft=“@+id/button1” 与button1控件左边平齐
  6. android:layout_alignRight=“@+id/button1” 与button1控件右边平齐
  7. android:layout_alignTop=“@+id/button1” 与button1控件上边平齐
  8. android:layout_alignBottom=“@+id/button1” 与button1控件下边平齐

在这里插入图片描述

RelativeLayout的常见用法:

第一种情况:常用的列表页布局:
在这里插入图片描述

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        // 标题
        <include
            android:id="@+id/title_layout"
            layout="@layout/title_layout"
        />

        // 1.android:layout_below="@+id/title_layout";这个布局在title的下方
        // 2.android:layout_marginBottom="60dp";指定该属性所在控件距下部最近控件的最小值;
        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/sw_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/title_layout"
            android:layout_marginBottom="60dp">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

        // 3.android:layout_alignParentBottom="true";在父容器底部
        <LinearLayout
            android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/material_apply"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
             />

        </LinearLayout>

    </RelativeLayout>

第二种情况:常用的搜索抽屉布局:
搜索栏有很多,有滚动条,重置与搜索按钮在最底部。
在这里插入图片描述

    <RelativeLayout>
        // 1. 添加滚动条
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingHorizontal="20dp">
            // 2. 搜索栏需要 占满全屏 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    style="@style/select_title"
                    android:text="@string/material_type" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/madetails_recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp" />

                <TextView
                    style="@style/select_title"
                    android:layout_marginTop="20dp"
                    android:text="@string/name_of_goods" />

                <EditText
                    android:id="@+id/madetails_name_of_goods"
                    style="@style/select_ed"
                    android:background="@drawable/select_ed_background"
                    android:hint="@string/name_of_goods" />

                <TextView
                    style="@style/select_title"
                    android:layout_marginTop="20dp"
                    android:text="@string/model_of_goods" />

                <EditText
                    android:id="@+id/madetails_model_of_goods"
                    style="@style/select_ed"
                    android:background="@drawable/select_ed_background"
                    android:hint="@string/model_of_goods" />

                <TextView
                    style="@style/select_title"
                    android:layout_marginTop="20dp"
                    android:text="@string/specification_of_goods" />

                <EditText
                    android:id="@+id/madetails_specification_of_goods"
                    style="@style/select_ed"
                    android:background="@drawable/select_ed_background"
                    android:hint="@string/specification_of_goods" />

                <TextView
                    style="@style/select_title"
                    android:layout_marginTop="20dp"
                    android:text="@string/code_of_goods" />

                <EditText
                    android:id="@+id/madetails_code_of_goods"
                    style="@style/select_ed"
                    android:background="@drawable/select_ed_background"
                    android:hint="@string/code_of_goods" />

            </LinearLayout>


        </androidx.core.widget.NestedScrollView>

        // 3. android:layout_alignParentBottom="true";相对于根元素布局,在根元素的底部。
        <LinearLayout
            style="@style/select_bottom_layout"
            android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/reset_btn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="@color/dark_blue"
                android:text="@string/reset"
                android:textColor="@color/white"
                android:textSize="22dp" />

            <Button
                android:id="@+id/filter_btn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="@color/colorAccent"
                android:text="@string/filter"
                android:textColor="@color/white"
                android:textSize="22dp" />

        </LinearLayout>
    </RelativeLayout>

类似于这种布局的做法:1.上面的元素需要占满全屏 2.下面的按钮 android:layout_alignParentBottom="true" 相对父元素布局,在父元素的底部。

第三种情况:子页面展示项
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--1. android:layout_alignParentLeft="true": 父容器左边
				   android:layout_alignParentRight="true" 父容器右边
				   android:layout_centerInParent="true" 水平垂直都居中
            -->
            <RelativeLayout 
            	android:layout_width="match_parent"
            	android:layout_height="60px">

                <TextView
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
            		android:layout_height="wrap_content"
                    android:text="测试" />

                <TextView
                    android:id="@+id/apply_form_tv"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
            		android:layout_height="wrap_content"
                    tools:text="测试" />

            </RelativeLayout>


            <!--测试-->
            <RelativeLayout style="@style/sub_line">

                <TextView
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
            		android:layout_height="wrap_content"
                    android:text="测试" />

                <TextView
                    android:id="@+id/apply_form_tv"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
            		android:layout_height="wrap_content"
                    tools:text="测试" />

            </RelativeLayout>

        </LinearLayout>

</RelativeLayout>

对于这种展示形布局或者说是提交型布局:1.<LinearLayout 标签可以让其下的子布局页面竖向排列 2.<RelativeLayout 标签:位于父容器左边与父容器右边,且水平竖直居中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/384311.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JavaWeb--RequestResponse

Request&Response1 Request和Response的概述2 Request对象2.1 Request继承体系2.2 Request获取请求数据2.2.1 获取请求行数据2.2.2 获取请求头数据2.2.3 获取请求体数据2.2.4 小结2.2.5 获取请求参数的通用方式2.3 IDEA快速创建Servlet2.4 请求参数中文乱码问题2.4.1 POST请…

智能家居Homekit系列一智能通断开关

智能通断器&#xff0c;也叫开关模块&#xff0c;可以非常方便地接入家中原有开关、插座、灯具、电器的线路中&#xff0c;通过手机App或者语音即可控制电路通断&#xff0c;轻松实现原有家居设备的智能化改造。 随着智能家居概念的普及&#xff0c;越来越多的人想将自己的家改…

WebRTC系列分享 | WebRTC视频QoS全局技术栈

概述目前总结出WebRTC用于提升QoS的方法有&#xff1a;NACK、FEC、SVC、JitterBuffer、IDR Request、Pacer、Sender Side BWE、Probe、VFR&#xff08;动态帧率调整策略&#xff09;、AVSync&#xff08;音视频同步&#xff09;、动态分辨率调整。这几种方法在WebRTC架构分布如…

上海亚商投顾:沪指窄幅震荡 ChatGPT概念再度走高

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。市场情绪沪指今日窄幅震荡&#xff0c;创业板指低开低走&#xff0c;午后跌幅扩大至1%&#xff0c;宁德时代一度跌近4%。6G概…

【架构师】零基础到精通——微服务治理

博客昵称&#xff1a;架构师Cool 最喜欢的座右铭&#xff1a;一以贯之的努力&#xff0c;不得懈怠的人生。 作者简介&#xff1a;一名Coder&#xff0c;软件设计师/鸿蒙高级工程师认证&#xff0c;在备战高级架构师/系统分析师&#xff0c;欢迎关注小弟&#xff01; 博主小留言…

【java基础】一篇文章彻底搞懂lambda表达式

文章目录lambda表达式是什么lambda表达式的语法函数式接口初次使用深入理解方法引用 :: 用法快速入门不同形式的::情况1 object::instanceMethod情况2 Class::instanceMethod情况3 Class::staticMethod对于 :: 的一些示例及其注意事项构造器引用变量作用域使用外部变量定义内部…

web自动化 基于python+Selenium+PHP+Ftp实现的轻量级web自动化测试框架

1、 开发环境 win7 64 PyCharm 4.0.5 setuptools-29.0.1.zip 下载地址&#xff1a;setuptools-29.0.1.zip_免费高速下载|百度网盘-分享无限制 官方下载地址&#xff1a;setuptools PyPI python 3.3.2 mysql-connector-python-2.1.4-py3.3-win64 下载地址&#xff1a;mysq…

企业 Active Directory 自助服务

您的企业是否正在寻找一个全面的 Active Directory 自助服务解决方案&#xff0c;使用户能够在没有帮助台帮助的情况下满足自己的 Active Directory 需求&#xff1f;ADSelfService Plus 了解您的安全问题&#xff0c;并提供基于审批的自助服务工作流功能&#xff0c;使管理员能…

内网vCenter部署教程一

PS&#xff1a;因为交换机链路为trunk&#xff0c;安装先登录ESXI&#xff0c;将端口组改为管理vlan ID&#xff08;1021&#xff09; 一、双击镜像&#xff0c;打开文件夹&#xff0c;目录为F:\vcsa-ui-installer\win32&#xff0c;双击installer.exe 二、先设置语言为中文 三…

机器学习笔记之狄利克雷过程(三)随机测度的生成过程(折棍子过程)

机器学习笔记之狄利克雷过程——随机测度的生成过程[折棍子过程]引言回顾&#xff1a;狄利克雷过程——定义随机测度的生成过程从随机测度的生成过程观察标签参数α\alphaα与随机测度离散程度之间的关系引言 上一节使用公式推导的方式介绍了狄利克雷过程中标量参数α\alphaα…

云服务器ECS 什么是云服务器ECS?

云服务器ECS&#xff08;Elastic Compute Service&#xff09;是阿里云提供的性能卓越、稳定可靠、弹性扩展的IaaS&#xff08;Infrastructure as a Service&#xff09;级别云计算服务。 云服务器ECS免去了您采购IT硬件的前期准备&#xff0c;让您像使用水、电、天然气等公共…

【线性DP】猴子与香蕉

可恶&#xff0c;就差一点就能独立写出这道题了&#xff01;4548. 猴子和香蕉 - AcWing题库题意&#xff1a;思路&#xff1a;设计状态的时候一开始不知道怎么设&#xff0c;后来试了一下发现这样设很合理因此在设状态的时候很多时候都要试一试当时间或空间吃不消时&#xff0c…

django ModelForm外鍵問題

背景 django在使用ModelForm時如果存在外鍵字段&#xff0c;默認是ChoiceField讓你選擇外鍵關聯表有的值&#xff0c;但是如果關聯表的數據很多的話選擇就很難找到選項。所以想能不能換成輸入框TextInput。 舉個例子 models.py 這裏建了兩個表&#xff0c;把用戶表的name作…

ERP的实施节省了公司人力吗?

业界一直有句老话&#xff1a;“不上ERP等死&#xff0c;上了ERP找死”。 可把ERP的尴尬处境说透了。 有人把ERP奉为信仰&#xff1a;“那些说ERP不好用的根本是没用明白。” 有人则认为ERP只是卖概念&#xff0c;冷嘲&#xff1a;“实施ERP的企业&#xff0c;估计一半都倒闭…

手机质保调到36个月,会对行业造成怎样的冲击?

2月17日&#xff0c;中国信通院发布数据显示&#xff0c;2022年全年国内市场手机出货量累计2.72亿部&#xff0c;同比下降22.6%。其中5G手机的出货量为2.14亿部&#xff0c;同比下降19.6%。这则数据打破了一些手机行业内的美好幻想&#xff1a;5G时代的到来&#xff0c;并没有涌…

哔哩哔哩自动生成视频上传,B站发布软件使用教程

哔哩哔哩自动生成视频上传&#xff0c;B站发布软件使用教程&#xff0c;全自动引流发帖软件介绍#引流发帖软件#全自动引流发帖#引流推广#拓客引流#爆粉软件 大家好&#xff0c;我是百收编辑狂潮老师&#xff0c;下面给大家讲一下 b 站上传软件它的一个使用方法。第一次使用的时…

几种常见的 JVM 调优场景

一、cpu占用过高 cpu占用过高要分情况讨论&#xff0c;是不是业务上在搞活动&#xff0c;突然有大批的流量进来&#xff0c;而且活动结束后cpu占用率就下降了&#xff0c;如果是这种情况其实可以不用太关心&#xff0c;因为请求越多&#xff0c;需要处理的线程数越多&#xff…

[Java·算法·困难]LeetCode25. K 个一组翻转链表

每天一题&#xff0c;防止痴呆题目示例分析思路1题解1分析思路2题解2分析思路3题解3&#x1f449;️ 力扣原文 题目 给你链表的头节点 head &#xff0c;每 k 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 k 是一个正整数&#xff0c;它的值小于或等于链表的长度。…

JavaWeb--用户注册登录案例

用户注册登录案例1 用户登录1.1 需求分析1.2 环境准备1.3 代码实现2 用户注册2.1 需求分析2.2 代码编写3 SqlSessionFactory工具类抽取目标&#xff1a; 能够完成用户登录注册案例的实现能够完成SqlSessionFactory工具类的抽取 接下来我们通过两个比较常见的案例&#xff0c;一…

国家能源局持续发文,赛宁网安以实力响应电力网络靶场建设

​​一、政策导向 ​​国家能源局连续三年发文 推进电力网络靶场建设 2021.1. 国家能源局印发《2021电力安全监管重点工作任务》提出&#xff1a;加强电网及网络安全监管。推进电力行业网络安全仿真验证环境&#xff08;靶场&#xff09;建设&#xff0c;组织开展电力行业网…