这几天手机安卓开发的学习,让我更加感受到了JAVA的魅力。
了解掌握了安卓开发软件的使用,接下来关键的第一步就是设计界面了。
界面设计包括布局和组件,组件按布局要求排列形成界面,而安卓的布局有以下五大布局:
【1】FrameLayout 框架布局,是布局文件中默认的最简单的布局。
所有添加到这个布局中的视图都以层叠的方式显示,且组件均显示在屏幕的左上角。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。在非常简单的界面中常用。
【2】LinearLayout 线性布局
在一个方向上(垂直或水平)对齐所有子元素,一个垂直列表每行将只有一个子元素(无论它们有多宽),一个水平列表只是一列的高度(最高子元素的高度来填充)。是常用的布局。
垂直线性布局:android:orientation="vertical"
水平线性布局:android:orientation="horizontal"
两者可嵌套使用。
例如:一个登录界面
<!--总体布局设计为垂直线性布局-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--嵌套水平布局,放置文本框和数字输入框-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_username"/>
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<!--嵌套水平布局,放置文本框和密码输入框-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_password" />
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</LinearLayout>
<!--登录按钮-->
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_login" />
</LinearLayout>
【3】RelativeLayout 相对布局
相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件,也可以是其它子控件,但是被参照的控件必须要在参照它的控件之前定义。个人觉得这种布局和线性布局比较好用,可常用到。
例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ttkupao.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageButton
android:id="@+id/jump"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/touch2"
android:onClick="click"/>
<ImageButton
android:id="@+id/down"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/touch"
android:onClick="click"/>
</RelativeLayout>
这是两个按钮放在屏幕的左下角和右下角位置,均是以父控件为参考的,这时没有必要将子控件之间再增加相对关系。
下面是相对布局的一些重要属性:
第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为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 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
【4】TableLayout(表格布局)
表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列,子组件全部按照水平线性排列,并且宽(match parent)高(wrap parent)一致,这样所有组件就像是排列在一个表格中,单元格可以为空,但是不可以跨列
【5】AbsoluteLayout(绝对布局)
android:layout_x="Xdp", android:layout_x="Xdp"指定组件的位置,即子控件需要指定相对于此坐标布局的横纵坐标值。由于手机应用需要适应不同的屏幕大小,而这种布局模型不能自适应屏幕尺寸大小,所以应用的相对较少。
具体的布局设计需要根据具体情况来设计,线性布局和相对布局是最常用到的。另外绘图的组件View或surfaceView若需要显示在其他组件之下,可在其他组件之前添加,否则可能被其他组件覆盖。
分享到:
相关推荐
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
下单系统的Spnigboot和微信小程序实现(全栈微信小程式下单)
该项目是一款基于Java的智能文件管家设计源码,涵盖102个文件,包括29个Java源文件、27个类文件、19个XML配置文件、10个YAML文件、8个列表文件、4个属性文件、4个JAR包文件以及1个Git忽略文件。该系统旨在提供高效便捷的文件管理解决方案。
基于YoloV8的简单目标检测和跟踪,使用KMNET进行鼠标移动(处理多目标移动抖动,处理鼠标平滑移动)
本项目是一款基于Vue和JavaScript开发的心旅途个性化推荐旅游平台设计源码,整合了513个Java文件、76个PNG图片、70个XML配置文件、62个JavaScript文件、42个Vue组件文件、28个CSS样式文件、22个HTML文件、18个YAML配置文件、16个属性文件、11个Vue模板文件,总计919个文件。平台采用现代化前端技术堆栈,旨在为用户提供个性化的旅游推荐服务。
AutoLine是一个基于Python的通用自动化测试开源平台,包含了657个文件,涵盖228个PNG图片、209个CSS样式、95个JavaScript脚本、39个Python源代码、21个HTML文件、19个XML文件、14个GIF图片、6个DS_Store文件、5个文本文件、4个Markdown文件。该平台的设计源码由多种编程语言编写,旨在提供灵活高效的自动化测试解决方案。
微信小程序图像裁剪工具_ e-cropper
【作品名称】:基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 传统的阅卷方式为流水线的手工作业方式。这样的方式存在很多的问题,因为很容易受到阅卷者的主观因素的影响,从而产生一定的偏差。所以很多人就讨论如何将人为的因素降低到最低,来确保考生的考试成绩的公平公正和准确。 基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域,分割,识别属于ABCD等,通过和实现设置好的标准答案excel对比,从而得出最终分数 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础能够看懂代码,能够自行调试代码并解决报错,能够自行添加功能修改代码。
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
本项目深入解析并实现了基于Java核心技术的Nacos配置中心,包含2707个文件,涵盖2180个Java源文件、177个JavaScript文件、52个XML文件、35个SCSS文件、22个PEM文件、20个属性文件、18个Markdown文件、16个Protocol Buffers文件、12个JSON文件、11个字体文件。项目涉及多种语言和技术,旨在提供一个全面的配置中心解决方案。
枝晶生长Comsol仿真模型。 锂枝晶生长过程的 枝晶生长Comsol仿真模型。 锂枝晶生长过程的枝晶形貌,温度场耦合,应力场,浓度场,电势场。 C++程序,基于元胞自动机法模拟枝晶生长,能实现任意角度(偏心正方算法),同时采用LBM考虑了对流作用对枝晶生长的影响
本项目为apple_pro客户关系管理系统的组件化开发源码,采用Python、CSS、HTML和JavaScript等多种语言编写,总计包含1078个文件。其中,Python源文件254个,Python编译后文件244个,CSS样式文件65个,HTML模板61个,JavaScript脚本40个,以及其他类型文件如LESS、SCSS、XML、PNG等。该系统通过组件化设计,旨在提升客户关系管理的效率与用户体验。
微信小程序日历插件_Calendar
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
另一个小型购物中心。Litemall=Spring Boot后端+Vue管理员前端+微信小程序用户前端+Vue用户移动端_stemall
该项目为基于GitHub的ESPnet语音处理工具包设计源码,包含10633个文件,涵盖Shell脚本、Python、MATLAB、C++等多种编程语言。文件类型包括2872个shell脚本、2303个YAML配置文件、1662个Python脚本、1567个配置文件、306个Markdown文件、223个Perl脚本、39个文本文件、35个Bash脚本、27个PNG图片、21个补丁文件。该项目定期更新,适用于语音处理领域的研究与开发。
该项目是一款基于Python开发的pyecharts可视化图表库源码,包含166个文件,涵盖了121个Python源文件、12个HTML文件、9个JSON文件、6个PNG图片文件、4个Markdown文件、3个文本文件、2个YAML文件以及少量配置和管理文件。该库旨在提供强大的数据可视化解决方案,适用于各种数据分析与展示需求。
STM32软件学习资料GPS与GPRSSTM32软件学习资料GPS与GPRS
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据