Android布局-LinearLayout、RelativeLayout

2023-11-04

在本教程中,我们将概述 Android 布局。我们还将探索一些可用于组织屏幕内容的特定布局控件,即 Android LinearLayout 和 AndroidrelativeLayout。

安卓布局

用户界面的基本构建块是View从 View 类创建并占据屏幕上的矩形区域的对象。视图是 UI 组件(如 TextView、Button、EditText 等)的基类。视图组是 View 的子类。一个或多个视图可以组合在一起形成一个视图组。 ViewGroup 提供了 android 布局,我们可以在其中对视图的外观和顺序进行排序。 ViewGroup 的示例有LinearLayout, FrameLayout, RelativeLayout etc.

Android 布局类型

Android 提供以下 ViewGroup 或布局:

  1. LinearLayout: 是一个 ViewGroup,它将所有子项沿单个方向(垂直或水平)对齐
  2. RelativeLayout: 是一个ViewGroup,以相对位置显示子视图
  3. AbsoluteLayout:允许我们指定子视图和小部件的确切位置
  4. TableLayout: 是一个将其子视图分组为行和列的视图
  5. FrameLayout: 是屏幕上的占位符,用于显示单个视图
  6. ScrollView:是一种特殊类型的 FrameLayout,它允许用户滚动浏览比物理显示占用更多空间的视图列表。 ScrollView 只能包含一个子视图或 ViewGroup,通常是一个 LinearLayout
  7. ListView: 是一个视图组,显示可滚动项目的列表
  8. GridView:是一个以二维滚动网格显示项目的ViewGroup。网格中的项目来自与该视图关联的 ListAdapter

在本教程中,我们将重点关注两种最常用的 Android 布局:

  1. 线性布局
  2. 相对布局

Android 布局属性

  1. 安卓:id:这是唯一标识视图的ID
  2. 机器人:布局宽度:这是布局的宽度
  3. 机器人:布局高度:这是布局的高度
  4. 机器人:布局边距:这是视图之外的额外空间。例如,如果你给android:marginLeft=20dp,那么视图将被排列在距左侧 20dp 之后
  5. 安卓:布局填充: 这类似于机器人:布局边距除了它指定了额外的空间inside风景
  6. 机器人:布局重力:指定子视图的定位方式
  7. 机器人:布局权重:这指定布局中应将多少额外空间分配给视图
  8. 安卓:layout_x:指定布局的 x 坐标
  9. 安卓:layout_y:指定布局的 y 坐标

安卓:布局宽度=包装内容告诉视图根据其内容所需的尺寸调整自身大小。安卓:布局宽度=匹配父级告诉视图变得和它的父视图一样大。

查看标识

XML 标记内 ID 的语法为:

  • 字符串开头的 at 符号 (@) 指示 XML 解析器应解析并扩展 ID 字符串的其余部分,并将其标识为 ID 资源
  • 加号 (+) 表示这是一个必须创建并添加到我们的资源中的新资源名称

Android 线性布局

Android LinearLayout 沿单行组织元素。我们可以使用指定该线是垂直还是水平android:orientation。默认情况下方向是水平的。垂直 LinearLayout 每行只有一个子元素(因此它是一列单个元素),水平 LinearLayout 在屏幕上只有一行元素。机器人:布局权重属性描述了元素的重要性。权重较大的元素占据更多的屏幕空间。下面是使用 LinearLayout 的布局 XML 示例:layout_linear.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="@dimen/activity_horizontal_margin">
    <Button
        android:id="@+id/backbutton"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="Row 2"
        android:layout_width="wrap_content"
        android:textSize="18sp"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />
    <TextView
        android:text="Row 3"
        android:textSize="18sp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="Row 4"
        android:textSize="18sp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:text="Row 5"
        android:textSize="18sp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/next_button"
            android:text="next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="Row 6b"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="Row 6c"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="Row 6d"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

在这个布局中我们有一个父级LinearLayout它具有垂直方向,包含按钮、文本视图和嵌套线性布局(具有水平方向)作为子视图。Note:嵌套布局不必是一种类型。例如,我们可以将 LinearLayout 作为relativelayout 的子级之一,反之亦然。

Android 相对布局

AndroidrelativeLayout 根据元素之间的关系以及元素与父容器的关系来布局元素。这是最复杂的布局之一,我们需要几个属性才能真正获得我们想要的布局。也就是说,使用RelativeLayout我们可以将视图定位到toLeftOf, 到右边, below or above它的兄弟姐妹。我们还可以相对于其父视图定位视图,例如水平居中, 垂直或两者,或与父级的任何边缘对齐RelativeLayout。如果子视图上没有指定这些属性,则默认情况下该视图将渲染到左上角位置。

Android 相对布局属性

以下是跨领域使用的主要属性相对布局。它们分为三个不同的类别:

相对于容器

  • android:layout_alignParentBottom :将元素的底部放置在容器的底部
  • android:layout_alignParentLeft :将元素的左侧放置在容器的左侧
  • android:layoutalignParentRight :将元素的右侧放置在容器的右侧
  • android:layout_alignParentTop :将元素放置在容器的顶部
  • android:layout_centerHorizo​​ntal :将元素在其父容器内水平居中
  • android:layout_centerInParent :将元素在其容器内水平和垂直居中
  • android:layout_centerVertical :将元素在其父容器内垂直居中

相对于兄弟姐妹

  • android:layout_above :将元素放置在指定元素上方
  • android:layout_below :将元素放置在指定元素下方
  • android:layout_toLeftOf :将元素放置到指定元素的左侧
  • android:layout_toRightOf :将元素放置在指定元素的右侧

@id/XXXXX” 用于通过元素的 id 引用元素。要记住的一件事是,在声明元素之前引用该元素会产生错误,因此在这种情况下应使用 @+id/。

与其他元素对齐

  • android:layout_alignBaseline :将新元素的基线与指定元素的基线对齐
  • android:layout_alignBottom :将新元素的底部与指定元素的底部对齐
  • android:layout_alignLeft :将新元素的左边缘与指定元素的左边缘对齐
  • android:layout_alignRight :将新元素的右边缘与指定元素的右边缘对齐
  • android:layout_alignTop :将新元素的顶部与指定元素的顶部对齐

下面的xml布局使用RelativeLayout: layout_relative.xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/backbutton"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/firstName"
        android:text="First Name"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/backbutton" />
    <TextView
        android:id="@+id/editFirstName"
        android:text="JournalDev"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/firstName"
        android:layout_below="@id/backbutton"/>
    <TextView
        android:id="@+id/editLastName"
        android:text="Layout Tutorial Example"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/lastName"
        android:layout_toRightOf="@+id/lastName"
        android:layout_toEndOf="@+id/lastName" />
    <TextView
        android:id="@+id/lastName"
        android:text="Last Name"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/firstName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp" />

    <Button
        android:id="@+id/next"
        android:text="Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editLastName"
        android:layout_alignLeft="@+id/editLastName"
        android:layout_alignStart="@+id/editLastName"
        android:layout_marginTop="37dp" />
</RelativeLayout>

正如您所看到的,我们可以根据元素的相对位置重新排列元素。以下 xml 布局表示具有嵌套线性布局和相对布局的自定义布局。layout_mixed.xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/parent_rl"
        android:text="Parent RelativeLayout"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout"
        android:layout_below="@id/parent_rl"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:text="Nested Horizontal"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="LL"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


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

            <TextView
                android:text="Double Nested"
                android:textSize="18sp"
                android:layout_margin="10dp"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:text="Vertical"
                android:textSize="18sp"
                android:layout_margin="10dp"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:text="LL"
                android:textSize="18sp"
                android:layout_margin="10dp"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>


    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/linearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Nested Relative Layout"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

        <Button
            android:id="@+id/back_button_pressed"
            android:text="back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="66dp" />

    </RelativeLayout>


</RelativeLayout>

Android布局项目结构

android layout, LinearLayout, RelativeLayout This project consists of three activities and the respective layouts that were discussed above.

Android布局代码

应用程序启动到 MainActivity,加载layout_linear.xml内容由以下代码组成:

package com.journaldev.layouts;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    Button back,next;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_linear);
        back=(Button)findViewById(R.id.back_button);
        next=(Button)findViewById(R.id.next_button);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

}

The SecondActivity and ThirdActivity加载layout_relative.xml and layout_mixed.xml布局分别如下图:

package com.journaldev.layouts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {
    Button back,next;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_relative);
        back=(Button)findViewById(R.id.backbutton);
        next=(Button)findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SecondActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

    }
}
package com.journaldev.layouts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ThirdActivity extends Activity {
    Button back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mixed);
        back=(Button)findViewById(R.id.back_button_pressed);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ThirdActivity.this,SecondActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

    }
}

The image outputs of the three layout files are shown below: layout_linear.xml android LinearLayout example As you can see the Parent LinearLayout consists of 6 child elements in a single vertical column among which one is a nested LinearLayout child view containing 4 components in horizontal orientation. layout_relative.xml android relativelayout example The arrows pointing in the image above depict how siblings are positioned relative to each other and relative to the container. layout_mixed.xml android layout example This Relative Layout consists of a Vertical LinearLayout within a Nested Horizontal LinearLayout along with a Child RelativeLayout. Note: Components belonging to different layouts are not siblings and hence can’t be positioned relative to each other. It’s their container layouts that are siblings and can be positioned relative to each other. If you are wondering about the blue lined rectangles and arrows, it’s because the images are from xml layouts in graphical view. When you will run the app, these blue lines and rectangles will not be shown. This brings an end to android layout tutorial. We’ll cover the other android layouts in the next tutorials. You can download the final Android Layout Project from the link below.

下载Android LinearLayoutrelativeLayout示例项目

参考:API Doc

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android布局-LinearLayout、RelativeLayout 的相关文章

随机推荐

  • 如何在 Linux 上创建可启动的 CentOS U 盘

    本教程介绍如何从 Linux 终端创建可启动的 CentOS USB 记忆棒 您可以使用此 USB 记忆棒在任何支持从 USB 启动的计算机上启动并测试或安装 CentOS 先决条件 8GB 或更大的 USB 记忆棒驱动器 运行任何 Lin
  • 如何撤消上次 Git 提交

    有时 在使用 Git 时 您可能想要撤消最新的提交 提交是 Git 存储库在给定时间的快照 Git 有一个名为的引用变量HEAD它指向当前工作分支中的最新提交 要撤消提交 您需要做的就是指向HEAD变量到前一个快照 本指南解释了如何撤消上次
  • 如何在 CentOS 7 上安装 Asterisk

    Asterisk 是最流行和广泛采用的开源 PBX 平台 为 IP PBX 系统 会议服务器和 VoIP 网关提供支持 它被世界各地的个人 小型企业 大型企业和政府使用 Asterisk 功能包括语音邮件 等待音乐 电话会议 呼叫排队 通话
  • 如何在 MySQL 中显示/列出用户

    您是否曾经需要获取 MySQL 服务器中所有用户的列表 有命令显示数据库和表 但没有MySQLshow users命令 本教程介绍如何通过命令行列出 MySQL 数据库服务器中的所有用户帐户 我们还将向您展示如何找出哪些用户有权访问给定的数
  • 如何在 Linux 中创建组(groupadd 命令)

    在 Linux 中 组用于组织和管理用户帐户 组的主要目的是定义一组权限 例如读 写或执行允许对于可以在组内的用户之间共享的给定资源 在本文中 我们将讨论如何在 Linux 中使用groupadd命令 groupadd命令语法 的一般语法为
  • Linux中的重命名命令(重命名多个文件)

    使用以下命令重命名多个文件和目录mv命令可能是一个乏味的过程 因为它涉及使用管道编写复杂的命令 loops 等等 这就是rename命令派上用场 它通过将名称中的搜索表达式替换为指定的替换来重命名给定的文件 在本教程中 我们将解释如何使用r
  • 如何在 CentOS 8 上安装 Vagrant

    Vagrant是用于构建和管理虚拟化开发环境的命令行工具 默认情况下 Vagrant 可以在 VirtualBox Hyper V 和 Docker 之上配置计算机 可以通过 Vagrant 插件系统启用对 Libvirt KVM VMwa
  • 如何在 Debian 10 Linux 上安装和使用 Curl

    Curl 是一个命令行实用程序 用于从远程服务器传输数据或向远程服务器传输数据 它允许您使用 HTTP HTTPS SCP SFTP and FTP协议 如果您尝试使用下载文件curl并收到一条错误消息说curl command not f
  • Linux 中的 Chmod 命令(文件权限)

    在 Linux 中 对文件的访问是通过文件权限 属性和所有权来管理的 这确保只有授权的用户和进程才能访问文件和目录 本教程介绍了如何使用chmod命令更改文件和目录的访问权限 Linux 文件权限 在进一步讨论之前 我们先解释一下基本的 L
  • 在 Linux 上创建可启动 Debian 10 USB 记忆棒

    本教程解释了如何使用以下命令从 Linux 终端创建可启动的 Debian 10 Buster USB 记忆棒dd命令 USB 记忆棒可用于在任何支持从 USB 启动的计算机上启动和安装 Debian 先决条件 1GB 或更大的 USB 记
  • Linux 中的 sudo 命令

    sudo 命令允许您以另一个用户 默认为 root 用户 运行程序 如果您在命令行上花费大量时间 那么 sudo 是您经常使用的命令之一 使用 sudo 而不是以 root 身份登录更安全 因为您可以向单个用户授予有限的管理权限 而无需他们
  • 如何在 Ubuntu 18.04 上设置 Nginx 服务器块

    Nginx Server Blocks 允许您在一台机器上运行多个网站 使用服务器块 您可以指定站点文档根 包含网站文件的目录 为每个站点创建单独的安全策略 为每个站点使用不同的 SSL 证书等等 在本文中 我们将提供有关如何在 Ubunt
  • 如何在 CentOS 7 上使用 Nginx 安装 WordPress

    WordPress 是全球最受欢迎的开源博客和 CMS 平台 为当今互联网上四分之一的网站提供支持 它基于 PHP 和 MySQL 并包含大量可以通过免费和高级插件和主题进行扩展的功能 WordPress 是创建在线商店 网站或博客的最简单
  • 如何在 CentOS 7 上安装 OpenCV

    OpenCV 开源计算机视觉库 是一个开源计算机视觉库 绑定了 C Python 和 Java 并支持所有主要操作系统 它可以利用多核处理并具有 GPU 加速功能来实现实时操作 OpenCV 的应用非常广泛 包括医学图像分析 拼接街景图像
  • 您应该了解的 Apache 命令

    Apache HTTP 服务器是世界上最流行的 Web 服务器 它是一个免费 开源 跨平台的 HTTP 服务器 提供强大的功能 可以通过各种模块进行扩展 如果您是开发人员或系统管理员 您很可能经常与 Apache 打交道 在本指南中 我们将
  • 如何在 Debian 10 Linux 上安装和使用 Docker

    Docker 是一个容器化平台 允许您快速构建 测试和部署应用程序作为可移植 自给自足的容器 几乎可以在任何地方运行 在本教程中 我们将解释如何在 Debian 10 Buster 上安装 Docker 并探索基本的 Docker 概念和命
  • Linux 中的 W 命令

    在这篇文章中 我们将讨论w命令 w是一个命令行实用程序 显示有关当前登录用户以及每个用户正在执行的操作的信息 它还提供有关系统已运行多长时间 当前时间和系统平均负载的信息 如何使用w命令 语法为w命令如下 w OPTIONS USER Wh
  • 如何在 CentOS 7 上安装 Skype

    Skype是世界上最受欢迎的通信应用程序之一 可让您免费进行在线音频和视频通话 并以经济实惠的价格拨打全球手机和固定电话 Skype 不是开源应用程序 也不包含在 CentOS 存储库中 本教程介绍如何在 CentOS 7 上安装最新版本的
  • 如何在 Ubuntu 18.04 上设置 OpenVPN 服务器

    无论您是想在连接不可信的公共 Wi Fi 网络时安全可靠地访问互联网 绕过地理限制内容还是允许您的同事在远程工作时安全地连接到您的公司网络 使用 VPN 都是最佳解决方案 VPN 允许您连接到远程 VPN 服务器 使您的连接加密且安全 并通
  • Android布局-LinearLayout、RelativeLayout

    在本教程中 我们将概述 Android 布局 我们还将探索一些可用于组织屏幕内容的特定布局控件 即 Android LinearLayout 和 AndroidrelativeLayout 安卓布局 用户界面的基本构建块是View从 Vie