【android】补充

3.3 常用布局
本节介绍常见的几种布局用法,包括在某个方向上顺序排列的线性布局,参照其他视图的位置相对排列的相对布局,像表格那样分行分列显示的网格布局,以及支持通过滑动操作拉出更多内容的滚动视图。

3.3.1 线性布局LinearLayout
前几个小节的例程中,XML文件用到了LinearLayout布局,它的学名为线性布局。顾名思义,线性布局像是用一根线把它的内部视图串起来,故而内部视图之间的排列顺序是固定的,要么从左到右排列,要么从上到下排列。在XML文件中,LinearLayout通过属性android:orientation区分两种方向,其中从左到右排列叫作水平方向,属性值为horizontal;从上到下排列叫作垂直方向,属性值为vertical。如果LinearLayout标签不指定具体方向,则系统默认该布局为水平方向排列,也就是默认除了方向之外,线性布局还有一个权重概念,所谓权重,指的是线性布局的下级视图各自拥有多大比例的宽高。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><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>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><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>

3.3.2 相对布局RelativeLayout
线性布局的下级视图是顺序排列着的,另一种相对布局的下级视图位置则由其他视图决定。相对布局名为RelativeLayout,因为下级视图的位置是相对位置,所以得有具体的参照物才能确定最终位置。如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。用于确定下级视图位置的参照物分两种,一种是与该视图自身平级的视图;另一种是该视图的上级视图(也就是它归属的RelativeLayout)。综合两种参照物,相对位置在XML文件中的属性名称说明见表。

这里将这些属性分成组,便于理解和记忆。
  a)、第一类:属性值为true或false
  android:layout_centerHrizontal 水平居中
  android:layout_centerVertical 垂直居中
  android:layout_centerInparent 相对于父元素完全居中
  android:layout_alignParentBottom 贴紧父元素的下边缘
  android:layout_alignParentLeft 贴紧父元素的左边缘
  android:layout_alignParentRight 贴紧父元素的右边缘
  android:layout_alignParentTop 贴紧父元素的上边缘

b)、第二类:属性值必须为id的引用名“@id/id-name”
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左边
  android:layout_toRightOf 在某元素的右边
  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

c)、第三类:属性值为具体的像素值,如30dip,40px
  android:layout_marginBottom 离某元素底边缘的距离
  android:layout_marginLeft 离某元素左边缘的距离
  android:layout_marginRight 离某元素右边缘的距离
  android:layout_marginTop 离某元素上边缘的距离
 

<?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="250dp"><TextViewandroid:id="@+id/tv_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="#EF3A3A"android:text="我在中间"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:background="#ffffff"android:text="水平中间"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_center_vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:background="#ffffff"android:text="我在垂直中间"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_parent_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:background="#ffffff"android:text="我跟上级左边对齐"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_parent_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:background="#ffffff"android:text="我跟上级右边对齐"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_parent_top"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:background="#ffffff"android:text="我跟上级顶部对齐"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_parent_bottom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="#ffffff"android:text="我跟上级底部对齐"android:textColor="#000000"android:textSize="11sp" /><TextViewandroid:id="@+id/tv_left_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@id/tv_center"android:layout_alignTop="@id/tv_center"android:background="#ffffff"android:text="我在中间左边"android:textColor="#009688"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_right_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/tv_center"android:layout_alignBottom="@id/tv_center"android:background="#ffffff"android:text="我在中间右边"android:textColor="#FF5722"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_above_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/tv_center"android:layout_alignLeft="@id/tv_center"android:background="#ffffff"android:text="我在中间上面"android:textColor="#FFEB3B"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_below_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/tv_center"android:layout_alignRight="@id/tv_center"android:background="#ffffff"android:text="我在中间下面"android:textColor="#FFEB3B"android:textSize="20sp" /></RelativeLayout>

3.3.3 网格布局GridLayout
虽然线性布局既能在水平方向排列,也能在垂直方向排列,但它不支持多行多列的布局方式,只支持单行(水平排列)或单列(垂直排列)的布局方式。若要实现类似表格那样的多行多列形式,可采用网格布局GridLayout。 网格布局默认从左往右、从上到下排列,它先从第一行从左往右放置下级视图,塞满之后另起一行放置其余的下级视图,如此循环往复直至所有下级视图都放置完毕。为了判断能够容纳几行几列,网格布局新增了android:columnCount与android:rowCount两个属性,其中columnCount指定了网格的列数,即每行能放多少个视图;rowCount指定了网格的行数,即每列能放多少个视图。

下面是运用网格布局的XML布局样例,它规定了一个两行两列的网格布局,且内部容纳四个文本视图。

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:columnCount="2"android:rowCount="2"><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#ffcccc"android:gravity="center"android:text="浅红色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#ffaa00"android:gravity="center"android:text="橙色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#00ff00"android:gravity="center"android:text="绿色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#660066"android:gravity="center"android:text="深紫色"android:textColor="#000000"android:textSize="17sp" /></GridLayout>

3.3.4 滚动视图ScrollView
手机屏幕的显示空间有限,常常需要上下滑动或左右滑动才能拉出其余页面内容,可惜一般的布局节点都不支持自行滚动,这时就要借助滚动视图了。与线性布局类似,滚动视图也分为垂直方向和水平方向两类,其中垂直滚动视图名为ScrollView,水平滚动视图名为HorizontalScrollView。这两个滚动视图的使用并不复杂,主要注意以下 3 点:

( 1 )垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

( 2 )水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。

( 3 )滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child。
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"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="wrap_content"android:layout_height="match_parent"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.rhkb.cn/news/41478.html

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

相关文章

uv:Rust 驱动的 Python 包管理新时代

在 Python 包管理工具层出不穷的今天&#xff0c;pip、pip-tools、poetry、conda 等各有千秋。而今天要介绍的 uv&#xff0c;则是一款由 Astral 团队推出、采用 Rust 编写的全新工具&#xff0c;目标直指成为 “Python 的 Cargo”。它不仅在性能上表现优异&#xff0c;而且在功…

package.json版本前缀

前言 执行 npm i 下载依赖后&#xff0c;element-plus出现bug&#xff08;单页面多个date-picker同时开启&#xff09;&#xff0c;这是 v2.9.0 的问题&#xff0c;但是项目 package.json 中版本如下&#xff1a; "element-plus": "^2.7.6",乍一看并不是…

CSS+JS 堆叠图片动态交互切换

结合DeepSeek提供的代码&#xff0c;终于实现了堆叠两张图片动态循环切换&#xff0c;以下是代码&#xff1a; 通过绝对定位放了两张图片 <div class"col-lg-5" style"z-index: 40; position: relative;"><img src"images/banner_1.png&quo…

SpringCould微服务架构之Docker(2)

Docker和虚拟机的差别&#xff1a; 虚拟机是在操作系统中模拟硬件设备&#xff0c;然后运行另外一个操作系统。

好用的Markdown阅读编辑器Typora破解记录

Typora破解 一、下载Typora二、安装Typora三、破解Typora &#x1f600; 记录一下Typora破解记录&#xff0c;怕不常用忘记咯&#xff0c;感觉自己现在的脑子就像我的肠子一样&#xff0c;刚装进去就么得了。。。&#x1f614; Typroa算是用起来很舒服的Markdown阅读器了吧&am…

UI前端与数字孪生:打造智慧城市的双引擎

hello宝子们...我们是艾斯视觉擅长ui设计和前端数字孪生、大数据、三维建模、三维动画10年经验!希望我的分享能帮助到您!如需帮助可以评论关注私信我们一起探讨!致敬感谢感恩! 随着信息技术的飞速发展&#xff0c;智慧城市的概念逐渐从理论走向实践。智慧城市旨在通过运用物联网…

“征服HTML引号恶魔:“完全解析手册”!!!(quot;表示双引号)

&#x1f6a8;&#x1f4e2; "征服HTML引号恶魔&#xff1a;“完全解析手册” &#x1f4e2;&#x1f6a8; &#x1f3af; 博客引言&#xff1a;当引号变成"恶魔" &#x1f631; 是否遇到过这种情况&#xff1a; 写HTML时满心欢喜输入<div title"他…

k8s高可用集群安装

一、安装负载均衡器 k8s负载均衡器 官方指南 1、准备三台机器 节点名称IPmaster-1192.168.1.11master-2192.168.1.12master-3192.168.1.13 2、在这三台机器分别安装haproxy和keepalived作为负载均衡器 # 安装haproxy sudo dnf install haproxy -y# 安装Keepalived sudo yum …

node.js笔记

1. Node.js基本概念 1.1 什么是Node.js Node.js是一个开源、跨平台的JavaScript运行环境&#xff0c;广泛应用于各类项目。它基于Google Chrome的V8 JavaScript引擎&#xff0c;性能卓越。 Node.js在单个进程中运行&#xff0c;利用异步I/O操作避免阻塞&#xff0c;能高效处…

关于在vscode中的Linux 0.11 应用程序项目的生成和运行

首先我们需要需要查看镜像文件 查看软盘镜像文件 floppyb.img 中的内容 在 VSCode 的“Terminal”菜单中选择“Run Build Task...”&#xff0c;会在 VSCode 的顶部中间位置弹出一个 可以执行的 Task 列表&#xff0c;选择其中的“打开 floppyb.img”后会使用 Floppy Editor …

【JavaScript 简明入门教程】为了Screeps服务的纯JS入门教程

0 前言 0-1 Screeps: World 众所不周知&#xff0c;​Screeps: World是一款面向编程爱好者的开源大型多人在线即时战略&#xff08;MMORTS&#xff09;沙盒游戏&#xff0c;其核心机制是通过编写JavaScript代码来控制游戏中的单位&#xff08;称为“Creep”&#xff09;&#…

【CSS文字渐变动画】

CSS文字渐变动画 HTML代码CSS代码效果图 HTML代码 <div class"title"><h1>今天是春分</h1><p>正是春天到来的日子&#xff0c;花都开了&#xff0c;小鸟也飞回来了&#xff0c;大山也绿了起来&#xff0c;空气也有点嫩嫩的气息了</p>…

【论文阅读】基于思维链提示的大语言模型软件漏洞发现与修复方法研究

这篇文章来自于 Chain-of-Thought Prompting of Large Language Models for Discovering and Fixing Software Vulnerabilities 摘要 软件安全漏洞在现代系统中呈现泛在化趋势&#xff0c;其引发的社会影响日益显著。尽管已有多种防御技术被提出&#xff0c;基于深度学习&…

SpringMVC_day02

一、SSM 整合 核心步骤 依赖管理 包含 SpringMVC、Spring JDBC、MyBatis、Druid 数据源、Jackson 等依赖。注意点&#xff1a;确保版本兼容性&#xff08;如 Spring 5.x 与 MyBatis 3.5.x&#xff09;。 配置类 SpringConfig&#xff1a;扫描 Service 层、启用事务管理、导入…

基于ADMM无穷范数检测算法的MIMO通信系统信号检测MATLAB仿真,对比ML,MMSE,ZF以及LAMA

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1 ADMM算法 4.2 最大似然ML检测算法 4.3 最小均方误差&#xff08;MMSE&#xff09;检测算法 4.4 迫零&#xff08;ZF&#xff09;检测算法 4.5 OCD_MMSE 检测算法 4.6 LAMA检测算法 …

CSS动画

目录 一、核心概念与语法 1. keyframes 关键帧 2. animation 属性 二、动画调速函数&#xff08;animation-timing-function&#xff09; 1. 预设值 2. 贝塞尔曲线 3. 步进函数&#xff08;steps()&#xff09; 三、动画控制与交互 1. 暂停与恢复 2. JavaScript 控制…

架构思维:预约抢茅子架构设计

文章目录 案例&#xff1a;预约抢茅子复杂度分析商品预约阶段等待抢购阶段商品抢购阶段订单支付阶段 技术方案商品预约阶段一、基于 Redis 单节点的分布式锁方案1. 核心流程2. 关键设计点 二、Redis 单节点方案的局限性1. 单点故障风险2. 主从切换问题 三、多节点 Redis 实现高…

PHP大马的使用

BestShell/best_php_shell.php at master Kevil-hui/BestShell 这里用到的是这位师傅的大马&#xff08;主要是从头开始写一个大马实在太麻烦了&#xff09; 用pikachu靶场进行上传的测试 在这里传马&#xff0c;这个是简单的前端校验&#xff0c;bp抓包改后缀就好了 上传成…

HCI 清除 SCP纳管残留信息

项目场景&#xff1a; 一台测试HCI主机&#xff0c;之前有连接了SCP&#xff0c;由于环境变更&#xff0c;无法与SCP连通&#xff0c;HCI残留了SCP纳管信息 问题描述 集群管理中没有脱离SCP的选项 点击vmware 虚拟机 显示被接管 云安全中心也显示被接管 原因分析&#xff1a; …