Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )

news/2024/4/30 3:44:01/文章来源:https://blog.csdn.net/jiebaoshayebuhui/article/details/127670729

运行有问题或需要源码请点赞关注收藏后评论区留言

线性布局LinearLayout

顾名思义,线性布局像是用一根线把它的内部视图串起来,故而内部视图之间的排列顺序是固定的,要么从左到右,要么从上到下排列。通过属性android:orientation区分两种方向

下面通过一个实例讲解 效果如下

LinearLayoutActivity类代码如下

package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class LinearLayoutActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_linear_layout);}
}

 activity_linear_layoutXML文件如下

<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>

权重布局

指的是线性布局的下级视图各自拥有多大比例的宽高。通过属性android:layout_weight来表达

效果如下

layout_width 为0dp时则表示水平方向的权重

layout_height为0dp时则表示竖直方向的权重

 LinearWeightActivity类代码如下

package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class LinearWeightActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_linear_weight);}
}

activity_linear_weightXML文件代码如下

<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:background="#ff0000"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="横排第一个"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="横排第二个"android:textSize="17sp"android:textColor="#000000" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="100dp"android:background="#00ffff"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:text="竖排第一个"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:text="竖排第二个"android:textSize="17sp"android:textColor="#000000" /></LinearLayout></LinearLayout>

相对布局RelativeLayout

相对布局的下级视图位置由其他视图决定,所以得有具体的参照物才能确定最终位置,可以以和视图自身平级的视图作参照物,也可以以上级视图作为参照物 效果如下

RelativeLayoutActivity类代码如下

package com.example.chapter03;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class RelativeLayoutActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_relative_layout);}}

 activity_relative_layoutXML文件代码如下

<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:layout_centerInParent="true"android:background="#eeeeee"android:text="我在中间"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:background="#eeeeee"android:text="我在水平中间"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_center_vertical"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:background="#eeeeee"android:text="我在垂直中间"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_parent_left"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:background="#eeeeee"android:text="我跟上级左边对齐"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_parent_right"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:background="#eeeeee"android:text="我跟上级右边对齐"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_parent_top"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:background="#eeeeee"android:text="我跟上级顶部对齐"android:textColor="#000000" /><TextViewandroid:id="@+id/tv_parent_bottom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="#eeeeee"android:text="我跟上级底部对齐"android:textColor="#000000" /><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="#eeeeee"android:text="我在中间左边"android:textColor="#000000" /><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="#eeeeee"android:text="我在中间右边"android:textColor="#000000" /><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="#eeeeee"android:text="我在中间上面"android:textColor="#000000" /><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="#eeeeee"android:text="我在中间下面"android:textColor="#000000" />
</RelativeLayout>

网格布局GridLayout

若要实现类似表格那样的多行多列形式,可采用网格布局GridLayout,它默认从左往右,从上到下排列 效果如下

 GridLayoutActivity类代码如下

package com.example.chapter03;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class GridLayoutActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_grid_layout);}
}

activity_grid_layoutXML文件代码如下

<!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
<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="180dp"android:layout_height="60dp"android:gravity="center"android:background="#ffcccc"android:text="浅红色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#ffaa00"android:text="橙色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#00ff00"android:text="绿色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#660066"android:text="深紫色"android:textColor="#000000"android:textSize="17sp" /></GridLayout>

滚动视图ScrollView

手机屏幕的显示空间有限,常常需要上下滑动或左右滑动才能拉出其余页面内容,这时候就要借助滚动视图了。其中垂直滚动视图名为ScrollView,水平滚动视图名为HorizontalScrollView

原始效果如下

 滚动后效果如下

 垂直方向滚动时 layout_width属性值设置为match_parent layout_height属性值设置为wrap_content

水平方向滚动时  layout_width属性值设置为wrap_content layout_height属性值设置为match_parent

 ScrollViewActivity类代码如下

package com.example.chapter03;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class ScrollViewActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_scroll_view);}
}

activity_scroll_viewXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp --><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><!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 --><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.luyixian.cn/news_show_411609.aspx

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

相关文章

学完Python,不做程序员,只接兼职,哎,就是玩儿

现在这个时代&#xff0c;人人开始追求做斜杠青年&#xff0c;多方面开展副业&#xff0c;赚取几份工资。有很多朋友就抓住了Python的风口&#xff0c;靠着Python兼职月入一万。那么学完Python&#xff0c;有哪些可以做的兼职呢&#xff1f; 一、闲暇时间&#xff0c;接自由单…

DEFORMABLE DETR:用于端到端对象检测的可变形Transformer

论文&#xff1a;《DEFORMABLE DETR: DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION》 论文链接&#xff1a;https://arxiv.org/pdf/2010.04159.pdf 代码链接&#xff1a;https://github.com/fundamentalvision/Deformable-DETR 最近在目标检测领域提出了DETR&…

猿创征文|keil和Proteus使用教程

&#x1f4ac;推荐一款模拟面试、刷题神器 、从基础到大厂面试题&#xff1a;&#x1f449;点击跳转刷题网站进行注册学习 目录 一、keil的使用教程 1、新建一个项目 2、选择单片机型号 3、新建工程文件 二、Proteus的使用教程 1、新建一个工程 2、单片机型号及元器件的…

海藻酸钠-四嗪|TZ-PEG-alginate|海藻酸钠-peg-四嗪TZ

海藻酸钠-四嗪|TZ-PEG-alginate|海藻酸钠-peg-四嗪TZ 中文名称&#xff1a;海藻酸钠-聚乙二醇-四嗪 英文名称&#xff1a;TZ-PEG-alginate 纯度&#xff1a;95% 存储条件&#xff1a;-20C&#xff0c;避光&#xff0c;避湿 外观:固体或粘性液体&#xff0c;取决于分子量 …

【Web-CSS基础】CSS的三大特性、定位方式、静态定位、相对定位、绝对定位、浮动定位、固定定位、设置缩放动画

目录 CSS的三大特性 定位方式 静态定位 相对定位 绝对定位(absolute) 固定定位 浮动定位 粘性定位(sticky) 定位总结 综合练习 效果展示 目录 CSS的三大特性 定位方式 静态定位 相对定位 绝对定位(absolute) 固定定位 浮动定位 粘性定位(sticky) 定位总…

高分辨空间代谢组学测试的样品要求以及常见问题

高分辨空间代谢组学可实现定量检测&#xff0c;亦可定性检测&#xff0c;且可一次可同时检出多种类型的化合物&#xff0c;包括脂类、小分子代谢物、蛋白质、药物及其载体等&#xff0c;并且能够呈现出这些物质的空间分布情况。高分辨空间代谢组学测试的样品要求&#xff1a; …

五高引动三层需求 华为全屋智能3.0引领智能家居新进化

昨天,华为全屋智能3.0发布。从2020年11月发布全屋智能开始,华为不断迭代产品和体验,是全屋智能快速进化的主要推动者之一。这一次华为全屋智能3.0带来“高可靠、高掌控、高感官、高心意、高智能”的五高理念,更清晰定义了全屋智能,或者说空间智能该有的样子,这也将引领产…

GitHub榜一竟是Alibaba内部被疯狂转载的Spring全能指南?

spring相信大家都不会陌生! Spring 是目前主流的 Java Web 开发框架&#xff0c;是 Java 世界上最为成功的框架。该框架是一个轻量级的开源框架&#xff0c;具有很高的凝聚力和吸引力。 Spring 由 Rod Johnson 创立&#xff0c;2004 年发布了 Spring 框架的第一版&#xff0c;其…

自动控制原理 - 2 控制系统的数学模型 节2.7-2.10

2 控制系统的数学模型2.7 结构图的等效变换准则2.8 结构图等效变换的应用2.9 信号流图2.10 梅逊公式 2 控制系统的数学模型 2.7 结构图的等效变换准则 结构图没有直接给出系统输入与输出之间的定量关系。如何得到系统输入输出之间的传递函数&#xff0c;从而便于进一步分析系…

【LeetCode】No.78. Subsets -- Java Version

题目链接&#xff1a; 1. 题目介绍&#xff08;Subsets&#xff09; Given an integer array nums of unique elements, return all possible subsets (the power set). 【Translate】&#xff1a; 给定一个包含多个唯一元素的整数数组&#xff0c;返回所有可能的子集(幂集)。…

内部在看的Tomcat笔记,真不愧是阿里技术官

前言 SpringBoot中的Tomcat容器 SpringBoot可以说是目前最火的Java Web框架了。它将开发者从繁重的xml解救了出来&#xff0c;让开发者在几分钟内就可以创建一个完整的Web服务&#xff0c;极大的提高了开发者的工作效率。Web容器技术是Web项目必不可少的组成部分&#xff0c;…

学弟:手工测试和自动化测试的区别是啥?

一、 手工测试 1、 什么是手工测试&#xff1f; 手工测试是由测试工程师手动测试软件各项功能以发现缺陷的过程。测试人员应该从最终用户的角度出发&#xff0c;并确保所有功能都按照项目的需求文档中的说明工作。在此过程中&#xff0c;测试人员执行测试用例 并手动生成报告…

Word控件Spire.Doc 【文本】教程(11) ;如何将文本分成两列并在它们之间添加行

列被广泛用于设置页面布局&#xff0c;它可以将文本分成两列或多列&#xff0c;以便文本可以在同一页面上从一列流到下一列。使用 Spire.Doc&#xff0c;我们可以实现此功能并同时在列之间添加一条线。本文将介绍如何将文本拆分为两列并在它们之间添加行。 Spire.Doc for.NET …

图解 Redis 分布式锁,写得太好了!

分布式锁的演进 基本原理 我们可以同时去一个地方“占坑”&#xff0c;如果占到&#xff0c;就执行逻辑。否则就必须等待&#xff0c;直到释放锁。“占坑”可以去redis&#xff0c;可以去数据库&#xff0c;可以去任何大家都能访问的地方。等待可以自旋的方式。 阶段一 publi…

上海各梯队IB学校怎么选?

近日&#xff0c;随着各大国际学校开始公布秋招信息&#xff0c;第一轮秋招考试也将在本周末正式到来。 除了春招主力军A-level学校以外&#xff0c;许多IB和AP美高学校的秋招都格外收到关注。上海到底有哪些优质的IB学校&#xff1f;学生的IB成绩和升学情况如何&#xff1f;什…

中国房车产业深度调研及未来发展现状趋势预测报告

高消费人群的房车旅行新宠&#xff0c;百亿规模产业正在爆发。 随着人们收入和消费水平的提高&#xff0c;具有移动性、独立性、私密性等特点的房车旅游正成为新的热门中高端旅游产品。在小红的书里&#xff0c;与房车相关的笔记有40多万条。在Tik Tok的“房车”和“房车旅行”…

日本知名汽车零部件公司巡礼系列之株式会社104

株式会社104 业务内容&#xff1a; 汽车部件制造(刹车零件、发动机支架、其他支架等) 房屋部件制造 复印机等零件制造 公司简介&#xff1a; 成立时间&#xff1a;1978年3月 资本金&#xff1a;1000万日元&#xff08;2022年汇率约50万人民币&#xff09; 员工数&#x…

BSA-PEI,牛血清白蛋白-聚乙烯亚胺,BSA-聚乙烯亚胺的保存

产品名称&#xff1a;牛血清白蛋白-聚乙烯亚胺&#xff0c;BSA-聚乙烯亚胺 英文名称&#xff1a;BSA-PEI 用途&#xff1a;科研 状态&#xff1a;固体/粉末/溶液 产品规格&#xff1a;1g/5g/10g 保存&#xff1a;冷藏 储藏条件&#xff1a;-20℃ 储存时间&#xff1a;1年 温馨提…

68、SpringAQMP(消息转化器)

SpringAQMP&#xff08;消息转化器&#xff09; 第一步&#xff1a;查看我们的发送消息感觉都可以是java对象 第二步&#xff1a;在配置里声明一个object队列 第三步&#xff1a;发送一个对象的消息 测试&#xff1a; RbMQ最早只支持字节&#xff0c;这里spring运行我们发obj…

JavaWeb传统商城(MVC三层架构)的促销功能模块【进阶版】

文章目录一.JavaWeb商城项目的促销功能模块【进阶版】开发过程记录1.1 项目背景1.2 需求分析1.3 开发流程/顺序二.促销页面(0.1颗星)2.1 需求介绍2.2 JSP页面2.3效果展示三,商品详情页面(0.2颗星)3.1 需求介绍和效果图3.2 数据库分析3.2 Servlet层3.3 Service层3.4 DAO层3.5 JS…