Day-12 RelativeLayout

RelativeLayout(相对布局)
在RelativeLayout中,元件的位置可以是相对於整个RelativeLayout亦或是相对於其他元件的位置。

由於较难用文字说明,因此透过以下程序讲述

<?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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
        android:textSize="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="50dp"/>

    <Button
        android:id="@+id/B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        android:textSize="30dp"
        android:layout_below="@+id/A"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="30dp"/>

    <Button
        android:id="@+id/C"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        android:textSize="30dp"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/D"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="D"
        android:textSize="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="80dp"
        android:layout_marginBottom="30dp" />

</RelativeLayout>

这是所呈现的版面配置
https://ithelp.ithome.com.tw/upload/images/20210924/20141950Vgq0h23Kes.png

首先是ButtonA
android:layout_alignParentLeft及android:layout_alignParentLeft设定为true
代表该元件对齐父元件的左侧及上缘,而本程序的父元件即是整个画面
android:layout_marginLeft="60dp"及android:layout_marginTop="50dp"
分别设置了该元件左侧与上方边界的距离

再来是ButtonB
android:layout_below="@+id/A" 表示设定ButtonB位於ButtonA下方
因此此处的android:layout_marginTop="30dp" 为设置与ButtonA的边界为30dp
而非与画面上方边界的距离

接着是ButtonC
ButtonC设置android:layout_centerInParent为true
表示元件对齐於画面中央的位置

最後是ButtonD
与ButtonA相同,同样是对齐边界
只不过ButtonD为对齐画面右侧与底部


<<:  ASP.NET MVC 从入门到放弃(Day21)-MVC查询资料介绍

>>:  模型的内容01

Day 08 : Longest Mountain in Array

先来看简述题目的定义 至少要有连续3个以上的整数 从左往右看他要是严格递增直到这些数中的最大值(山顶...

19. STM32-CAN-BUS (下)

结构体介绍 CAN_FilterTypeDef typedef struct { uint32_t ...

从零开始的ASP.Net Core 学习

万事起头难 平常的学习方式都是遇到问题才去学、上哪门课学什麽、对哪个主题有兴趣才去学,现在想要有组织...

# Day 29 Page Migration (四)

文件 原文文件:Page migration 翻译: 监控迁移 ======== 下列事件 (计数器...

Groovy 语言和你 SAY HELLO!!

第十八天 各位点进来的朋友,你们好阿 小的不才只能做这个系列的文章,但还是希望分享给点进来的朋友,知...