Android布局控件之LinearLayout、RelativeLayout、GridLayout、ScrollView

线性布局(LinearLayout)

orientation

  • horizontal:水平从左往右
  • vertical:垂直从上到下
  • 若不指定orientation属性,默认为水平
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"tools:context=".LinearLayoutActivity"><!--tools只有在开发的时候才有用, 在这其实可以去掉--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第一个    "android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout></LinearLayout>

在这里插入图片描述

线性布局的权重

  • 指的是线性布局的下级视图各自拥有多大比例的宽高
  • 权重属性名叫layout_weight,但该属性不在LinearLayout节点设置,而在线性布局的直接下一级视图设置,表示该下级视图占据的宽高比例。
  • layout_width填0dp时,layout_weight表示水平方向的宽度比例
  • layout_height填0dp时,layout_weight表示垂直方向的高度比例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"tools:context=".LinearLayoutActivity"><!--tools只有在开发的时候才有用, 在这其实可以去掉--><!--<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第一个    "android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout>--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:text="横排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="2"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000"/><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:text="竖排第二个"android:textSize="17sp"android:textColor="#000000"/></LinearLayout></LinearLayout>

在这里插入图片描述

相对布局(RelativeLayout)

  • 相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:
    • 与该视图自身平级的视图
    • 该视图的上级视图(也就是它归属的RelativeLayout)
  • 如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角
    在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="150dp"><TextViewandroid:id="@+id/tv_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_centerInParent="true"android:text="我在中间"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_centerHorizontal="true"android:text="我在水平中间"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_center_vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_centerVertical="true"android:text="我在垂直中间"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_parent_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_alignParentLeft="true"android:text="我跟上级左边对齐"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_parent_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_alignParentRight="true"android:text="我跟上级右边对齐"android:textSize="11sp"android:textColor="#000000"/><!--“我跟上级顶部对齐”与”我跟上级左边对齐“的位置是一样的,会覆盖掉--><TextViewandroid:id="@+id/tv_parent_top"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_alignParentTop="true"android:text="我跟上级顶部对齐"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_parent_bottom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_alignParentBottom="true"android:text="我跟上级底部对齐"android:textSize="11sp"android:textColor="#000000"/><TextViewandroid:id="@+id/tv_left_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_toLeftOf="@id/tv_center"android:layout_alignBottom="@id/tv_center"android:text="我在中间左边"android:textSize="11sp"android:textColor="#000000"/><!--倒五行没有‘+’号,因为‘+id’代表是新的id倒四行保证上下对齐,不然会变到上面去。因为center只有一行,所以alignBottom和alignTop效果一样--><TextViewandroid:id="@+id/tv_above_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ffffff"android:layout_above="@id/tv_center"android:layout_alignLeft="@id/tv_center"android:text="我在中间上面"android:textSize="11sp"android:textColor="#000000"/><!--倒四行保证左边对齐,不然会变到最左边去。--></RelativeLayout>

在这里插入图片描述

网格布局(GridLayout)

  • 支持多行多列
  • 默认从左往右、从上到下,(也就是说,先从左往右,再从上到下)新增两个属性:
    • columnCount属性,它指定了网格的列数,即每行可以放几个视图
    • rowCount属性,他指定了网格的行数,即每列可以放几个
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:columnCount="2"android:rowCount="2"><!--超过2 * 2 = 4个后它会继续向下排,行数设定为2其实没有很大意义--><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#ffcccc"android:text="浅红色"android:gravity="center"android:textColor="#000000"android:textSize="17sp"/><!--gravity用来设置对齐方式,在这使其居中--><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#ffaa00"android:text="橙色"android:gravity="center"android:textColor="#000000"android:textSize="17sp"/><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#00ff00"android:text="绿色"android:gravity="center"android:textColor="#000000"android:textSize="17sp"/><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#660066"android:text="深紫色"android:gravity="center"android:textColor="#000000"android:textSize="17sp"/><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#660066"android:text="深紫色"android:gravity="center"android:textColor="#000000"android:textSize="17sp"/></GridLayout>

在这里插入图片描述

滚动视图(ScrollView)

  • 滚动视图有两种:
    • ScrollView:垂直方向,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。
    • HorizontalScrollView:水平方向,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><HorizontalScrollViewandroid:layout_width="wrap_content"android:layout_height="200dp"><!--水平方向的线性布局,两个子视图的颜色分别为青色和黄色--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:orientation="horizontal"><Viewandroid:layout_width="300dp"android:layout_height="match_parent"android:background="#aaffff" /><Viewandroid:layout_width="300dp"android:layout_height="match_parent"android:background="#ffff00" /></LinearLayout></HorizontalScrollView><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><!--垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="400dp"android:background="#00ff00" /><Viewandroid:layout_width="match_parent"android:layout_height="400dp"android:background="#ffffaa" /></LinearLayout></ScrollView></LinearLayout>

在这里插入图片描述

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

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

相关文章

什么是DDoS攻击

DDoS攻击 1. 定义2. DDoS攻击类型2.1 网络层攻击2.2 传输层攻击2.3 应用层攻击 3.DDoS攻击态势特点 1. 定义 分布式拒绝服务&#xff08;DDoS&#xff09;攻击是一种常见的网络攻击形式。攻击者利用恶意程序对一个或多个目标发起攻击&#xff0c;企图通过大规模互联网流量耗尽…

微软与 OpenAI 宫斗大戏背后的故事【番外详细剖析篇】

​​微软和 OpenAI 曾精心制定了一个协议&#xff0c;目的是既要雄心勃勃又要确保安全地发布人工智能产品。然而&#xff0c;OpenAI 的董事会突然打破了所有这些精心策划的计划。 在感恩节前一个星期五的上午 11:30 左右&#xff0c;微软的首席执行官 Satya Nadella 正在和高层…

Git 简介及异常场景处理

一、简介 介绍Git之前&#xff0c;还得先介绍下 版本控制系统&#xff08;VCS&#xff09;&#xff0c; 和它的发展历史 纵观版本控制系统的发展历史&#xff0c;广义上讲&#xff0c;版本控制工具的历史可以分为三代&#xff1a; 第一代 第一代版本控制系统被称为本地版本控…

隐式类型转换(整型提升和截断)、强制类型转换的总结

前言 在写了这么久的习题博客&#xff0c;我发现其中非常爱考查的一个知识就是类型的转换&#xff0c;比如不同类型之间的加减乘除、比较大小和赋值运算等&#xff0c;这里面涉及最为多的就是隐式类型转换&#xff0c;所以在此专门写一个关于类型转换的知识点总结&#xff0c;也…

文艺复兴!ICO或再次兴起?香港Web3崛起前五部曲之一!

近日&#xff0c;香港证券及期货专业总会发布了《2024至2025年度财政预算案》&#xff0c;提出了一系列举措&#xff0c;其中最引人注目的莫过于政府考虑推出ICO发行机制&#xff0c;这一预算案被广泛视为香港在Web3崛起前的文艺复兴五部曲之一&#xff0c;引发了业界和投资者的…

关于 ls -s 输出文件大小的单位问题的讨论(stat 和ls -s的块不一样的,只是名称相同而已)

自己看书正好看到这里&#xff0c;正纳闷呢&#xff0c;上网查了下&#xff0c;发现不是我自己在为这个问题感到困惑。 有个大哥提出一个问题&#xff1a; 问题标题&#xff1a; ls -s的单位到底是什么&#xff1f; man ls -s, --size print the alloca…

MyBatis的创建,简单易懂的一篇blog

文章目录 一、MyBatis是什么二、操作流程三.配置resource总结 一、MyBatis是什么 MyBatis 是⼀款优秀的持久层框架&#xff0c;它⽀持⾃定义 SQL、存储过程以及⾼级映射。MyBatis 去除了⼏乎所有的 JDBC 代码以及设置参数和获取结果集的⼯作。MyBatis 可以通过简单的 XML 或注…

python zmq客户端和服务端router socket类型

import zmq import threading import timeclass ZMQClient(threading.Thread):def __init__(self):super(ZMQClient, self).__init__()self._context zmq.Context()self._client_socket self._context.socket(zmq.ROUTER)# 客户端标识&#xff0c;服务端接收数据后&#xff0…

kali常用命令

1.常用命令 passwd 修改密码 passwd root 修改root用户密码 date 显示系统日期 sudo 后面加命令 就可以调用管理权限 apt-get update 更新软件列表 这个命令&#xff0c;会访问源列表里的每个网址&#xff0c;并读取软件列表&#xff0c;然后保存在本地电脑。我们在新立得软件包…

Azure Machine Learning - 使用 REST API 创建 Azure AI 搜索索引

本文介绍如何使用 Azure AI 搜索 REST AP和用于发送和接收请求的 REST 客户端以交互方式构建请求。 关注TechLead&#xff0c;分享AI全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验&#xff0c;同济本复旦硕&#xff0c;复旦机器人智能实验室成员&…

【计算机网络】14、DHCP

文章目录 一、概述1.1 好处 二、概念2.1 分配 IP2.2 控制租赁时间2.3 DHCP 的其他网络功能2.4 IP地址范围和用户类别2.5 安全 三、DHCP 消息3.1 DHCP discover message3.2 DHCP offers a message 如果没有 DHCP&#xff0c;IT管理者必须手动选出可用的 ip&#xff0c;这太耗时了…

TA-Lib学习研究笔记——Price Transform (五)

TA-Lib学习研究笔记——Price Transform &#xff08;五&#xff09; 1.AVGPRICE Average Price 函数名&#xff1a;AVGPRICE 名称&#xff1a;平均价格函数 语法&#xff1a; real AVGPRICE(open, high, low, close) df[AVGPRICE] tlb.AVGPRICE(df[open],df[high],df[low…

【Python基础】内存管理机制

1. Python的内存分配 何时分配内存 使用对象时自动分配&#xff0c;查看对象内存所占大小&#xff0c;可以使用sys.getsizeof() 何时释放内存 不再使用对象时自动释放&#xff0c;释放时机由解释器内部策略控制 对象内存使用划分 一部分用于存储对象的数据一部分用于存储对象…

php 中生成订单号

字母日期。。。。。。。 function setOrderNo($year 2011) {$yCode array(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);$orderSn $yCode[intval(date(Y)) - $year] . strtoupper(dechex(date(m))) . date(d) . substr(time(), -5) . s…

电子印章管理系统:是什么、3个平台推荐

说到印章&#xff0c;相信看过近现代电视剧的人都见过&#xff0c;一般在订立合约时最常用到&#xff0c;双方在合约上加盖印鉴&#xff0c;即代表着合约的成立。 我小时候还见过我父亲的印章&#xff0c;只是随着时代的发展&#xff0c;印章因为不易携带&#xff0c;容易被盗…

Java 使用zxing生成二维码

POM文件&#xff0c;引用 <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.google.zxing</groupId><ar…

java 猜数字游戏

package com.gaoce;import java.util.Random; import java.util.Scanner;/*** ClassName: GuessNumber* Package: com.org* Description: 猜数字游戏* Author: H* Create: 2023/12/1 16:26* Version: 1.0*/public class GuessNumber {Random random new Random();public int r…

C++标准库类型string基本成员函数用法

标准库类型string&#xff0c;基本函数成员用法详细讲解 文章目录 标准库类型string&#xff0c;基本函数成员用法详细讲解一、头文件二、string构造函数三、string赋值函数assign四、string拼接函数append五、string查找函数 find和 rfind六、string替换函数 replace七、strin…

二叉树OJ题目——C语言

LeetCode 104.二叉树的最大深度 1. 题目描述&#xff1a; 给定一个二叉树 root &#xff0c;返回其最大深度。 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;3示例…

CSS浅谈动画性能

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 目的一、举个栗子二、性能分析1.从图层分析2.性能分析 总结 目的 为了探究使用动画时&#xff0c;『transform』和『width、height、margin等』的差异 一、举个栗子…