RelativeLayout布局实验(Android Studio)

简介: RelativeLayout布局实验(Android Studio)

RelativeLayout布局实验(Android Studio)

activity_main.xml

<?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:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="false"
    android:layout_alignParentTop="false"
    android:layout_alignParentRight="false"
    android:layout_alignParentBottom="false"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="parentstart" />
    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="166dp"
        android:layout_toRightOf="@id/b1"
        android:text="parentright" />
    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/b1"
        android:layout_marginTop="622dp"
        android:text="parentbottom" />
    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="670dp"
        android:layout_toRightOf="@id/b3"
        android:text="parentleft" />
    <Button
        android:id="@+id/cen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/colorAccent"
        android:text="center" />
    <Button
        android:id="@+id/above"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/cen"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="160dp"
        android:text="above" />
    <Button
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cen"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="50dp"
        android:text="left" />
    <Button
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cen"
        android:layout_marginLeft="270dp"
        android:layout_marginBottom="0dp"
        android:text="right" />
    <Button
        android:id="@+id/below"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cen"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="20dp"
        android:text="below" />
</RelativeLayout>

RelativeLayout(相对布局)中,控件可以通过两类属性表示相对位置,一类表示与父容器的相对位置,另一类表示与其他控件之间的相对位置。不管是相对于父容器位置还是其他控件位置(容器),在设置好大致方向之后,一般都是需要再设置具体方向的距离而且一般是两个(上下一个、左右一个)。

一、大致方位:相对于父容器的属性,属性值选择 true 或者 false。

android:layout_centerInParent(在父布局的水平、垂直方向都居中)

android:layout_centerVertical(在父布局的垂直方向居中)

android:layout_centerHorizontal(在父布局的水平方向居中)

android:layout_alignParentTop(是否与父控件的顶端对齐)

android:layout_alignParentBottom(是否与父控件的底端对齐)

android:layout_alignParentLeft(是否与父控件的左侧对齐)

android:layout_alignParentRight(是否与父控件的右侧对齐)

二、大致方位:相对于其他控件的属性,属性值为控件的id。

android:layout_above(位于某控件上方)

android:layout_below(位于某控件下方)

android:layout_toLeftOf(位于某控件左方)

android:layout_toRightOf(位于某控件右方)

android:layout_alignTop(当前控件的顶部是否与某控件顶部对齐)

android:layout_alignBottom(当前控件的底部是否与某控件底部对齐)

android:layout_alignLeft(当前控件的左界面是否与某控件左界面对齐)

android:layout_alignRight(当前控件的右界面是否与某控件右界面对齐)

android:layout_alignBaseline(与某控件的文本内容在一条直线上)

三、具体方向的距离:属性值单位选择为dp。

android:layout_margin(和父布局或者父控件或者其他控件四周的距离)

android:layout_marginTop(当前控件的顶部和父布局或者父控件或者其他控件上端的距离)

android:layout_marginBottom(当前控件的底部和父布局或者父控件或者其他控件下端的距离)

android:layout_marginLeft(当前控件的左界面和父布局或者父控件或者其他控件左端的距离)

android:layout_marginRight(当前控件的右界面和父布局或者父控件或者其他控件右端的距离)


相关文章
|
7月前
|
XML 存储 Java
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
182 3
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
|
7月前
|
存储 消息中间件 人工智能
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
229 11
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
|
11月前
|
Android开发 Windows
Android studio 报错Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Connection refused: connect(已解决)
这是一篇关于解决Android Studio报错“Connect to 127.0.0.1:8888 failed: Connection refused”的文章。问题通常因系统代理设置被Android Studio自动保存导致。解决方法是找到系统中Android Studio使用的gradle.properties文件(位于Windows的C:\Users\你的电脑用户名\.gradle或Mac的/Users/.{你的用户目录}/.gradle),删除或注释掉多余的代理配置后保存并重新Sync项目。希望此经验能帮助快速解决同类问题!
2026 36
|
11月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
910 19
|
12月前
|
开发工具 Android开发 iOS开发
如何在Android Studio中配置Flutter环境?
如何在Android Studio中配置Flutter环境?
2729 160
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
435 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
前端开发 Java 编译器
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
461 36
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
|
SQL 人工智能 Dart
Android Studio的插件生态非常丰富
Android Studio的插件生态非常丰富
926 1
|
Ubuntu Linux Android开发
Android Studio支持多种操作系统
Android Studio支持多种操作系统
719 1
|
数据可视化 开发工具 Android开发
Android Studio
Android Studio
910 1

热门文章

最新文章