您好!欢迎来到源码码网

Android五大布局之相对布局RelativeLayout使用

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-08 13:15
  • 阅读:600

RelativeLayout 又称相对布局。从名称上可以看出,这种布局方式是以一种让组件以相对于容器或者相对于容器中的另一个组件的相对位置进行放置的布局方式。

RelativeLayout 布局提供了一些常用的布局设置属性用于确定组件在视图中的相对位置。下面列举了 RelativeLayout 相关属性及其所代表的含义。


RelativeLayout 布局常用属性


属性描述
android:layout_above="@id/xxx"将控件置于给定 ID 控件之上
android:layout_below="@id/xxx"将控件置于给定 ID 控件之下
android:layout_toLeftOf="@id/xxx"将控件的右边缘和给定 ID 控件的左边缘对齐
android:layout_toRightOf="@id/xxx"将控件的左边缘和给定 ID 控件的右边缘对齐
android:layout_alignBaseline="@id/xxx"将控件的 baseline 与给定 ID 的 baseline 对齐
android:layout_alignTop="@id/xxx"将控件的上边缘和给定 ID 控件的上边缘对齐
android:layout_alignBottom="@id/xxx"将控件的底边缘和给定 ID 控件的底边缘对齐
android:layout_alignLeft="@id/xxx" 将控件的左边缘和给定 ID 控件的左边缘对齐
android:layout_alignRight="@id/xxx"将控件的右边缘和给定 ID 控件的右边缘对齐
android:layout_alignParentLeft="true"将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true"将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"将控件置于父控件的中心位置
android:layout_centerHorizontal="true"将控件置于水平方向的中心位置
android:layout_centerVertical="true"将控件置于垂直方向的中心位置

实例 RelativeLayoutDemo 演示了相对布局的使用方法,其运行效果如图 5 所示。

RelativeLayout布局效果

图 5  RelativeLayout 布局效果

实例 RelativeLayoutDemo 中的布局文件 main.xml 代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>


  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent">


  5.    <TextView

  6.         android:id="@+id/label"

  7.         android:layout_width="fill_parent"

  8.         android:layout_height="wrap_content"

  9.         android:text="@string/hello" />


  10.    <EditText

  11.         android:id="@+id/enter"

  12.         android:layout_width="fill_parent"

  13.         android:layout_height="wrap_content"

  14.         android:layout_alignParentLeft="true"

  15.         android:layout_below="@+id/label" />


  16.    <Button

  17.         android:id="@+id/button1"

  18.         android:layout_width="wrap_content"

  19.         android:layout_height="wrap_content"

  20.         android:layout_alignParentRight="true"

  21.         android:layout_below="@+id/enter"

  22.         android:text="@string/butltext" />


  23.    <Button

  24.         android:id="@+id/ok"

  25.         android:layout_width="wrap_content"

  26.         android:layout_height="wrap_content"

  27.         android:layout_alignBottom="@+id/button1"

  28.         android:layout_alignParentLeft="true"

  29.         android:text="@string/but2text" />


  30. </RelativeLayout>

该 RelativeLayout 布局的过程如下:

1)放置一个 ID 为 label 的 TextView 组件。

2)通过 android:layout_below="@+id/label" 属性将 ID 为 enter 的组件 EditText 放置到 TextView 的下面。

3)在布局中加入一个 ID 为 button1 的 Button,通过 android:layout_below="@+id/enter" 属性将该 Button 放置到 enter 的下面,通过 android:layout_alignParentRight= "true" 属性将 Button 放置到相对布局的右侧。

4)在相对布局中加入一个名为 ok 的 Button,通过 android:layout_alignBottom="@+ id/button1" 属性将该 Button 底边与 button1 对齐,通过 android:layout_alignParentLeft ="true" 属性将该 Button 放置到布局的左边。

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • js实现一键复制的几种方法
  • js实现一键复制的几种方法
  • JS实现一键复制可以使用以下方法:1、使用document.execCommand()方法实现复制:function copyTextToClipboard(text) {  var textarea = document.createElement(”textarea”);  textarea.value =
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-05-18 13:44
  • 阅读:148
  • bootstrap ui框架能用在uniapp中吗?
  • bootstrap ui框架能用在uniapp中吗?
  • BootstrapUI框架通常是前端开发中的一种工具,它提供了一套预定义的CSS样式和组件,用于快速构建响应式布局的网页。然而,UniApp是一个使用Vue.js开发跨平台应用的框架,它可以用来开发iOS、Android、以及各种小程序和H5应用。
  • 互动社区
  • 来源:源码码网
  • 编辑:热度建站
  • 时间:2024-04-12 00:04
  • 阅读:299
  • css实现banner图由中心点动态放大效果
  • css实现banner图由中心点动态放大效果
  • 在日常的网页设计中,为了让网页增加一定的特效以达到交互的目的,我们尝尝会在网页中使用一些动画效果。今天来说说实现banner图由中心点动态放大效果,实现这个效果需要用到css中的动画:animation​和关键帧:@keyframes,具体示例如下:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-11 18:52
  • 阅读:305
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • 在网页开发中为了提升网页的交互效果,经常会用到使用js给网页增加一定的特效,下边就来说说使用js实现鼠标滑动到某个位置数字自动滚动增加的效果。其实这种效果有很多中解决办法,自己也可以去写,下边我们借助countUp.js来实现,关于这个js文件,我放在末尾:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-08 09:20
  • 阅读:401
  • 响应式网页设计思路及注意事项
  • 响应式网页设计思路及注意事项
  • 一、什么是响应式网页响应式网页设计就是让网页具有根据设备类型应用CSS样式的能力。设计:设想、计划。设计就是把想法实现。网页设计:按照一定的设计思路布局网页内容。传统网页设计:都是针对PC端浏览器而设计的,不具备查询设备的能力,更不能对多种访问设备做出响应。传统网页设计的弊端:在移动互联网时代,传统的网页设计不适合多屏幕时代。响应式网页设计应运而生。响应式网页设计是一种设计网页的思想/方法。响应:指让我们的网页能够自动查询用户的访问设备
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-02 11:24
  • 阅读:289
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部