新建UILayoutTest项目,并让Android Studio自动帮我们创建好活动,活动名和布局都使用默认值

线性布局

LinearLayout又称线性布局,是一种非常常用的布局

android:orientation属性用来指定线性布局的排列方式,有2个可选值:horizontal 和 vertical ,分别表示水平排列和垂直排列,若不指定,则默认是horizontal

下面我们来实践一下,修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity"

android:orientation="horizontal">

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 1"/>

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 2"/>

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 2"/>

需要注意的是如果 LinearLayout 的排列方式是horizontal,则内部控件的宽度layout_width就不能指定为match_parent,因为这样的话控件会占满屏幕,其它控件就没有位置可以放下了。同样的,如果排列方式是vertical,则内部控件的高度 layout_height 不能指定为match_parent

运行效果如下图所示

image

下面来学习android:layout_gravity属性,这个跟android:gravity很相似,它们的区别是:android:gravity是指文本在控件中的对齐方式,android:layout_gravity是控件在布局中的对齐方式。

android:layout_gravity的可选值和android:gravity差不多,但是需要注意的是LinearLayout排列方式是horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加或减少一个控件,水平方向上的长度都会改变,因而无法指定水平方向上的对齐方式。同理,当LinearLayout的排列方式vertical时,只有水平方向上的对齐方式才会有效

修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity"

android:orientation="horizontal">

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="top"

android:text="Button 1"/>

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:text="Button 2"/>

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:text="Button 2"/>

因为此时LinearLayout的排列方向是horizontal,所以里面的Button控件只能指定垂直方向上的排列方向,重新运行程序,效果如下所示

image

接下来我们来学习android:layout_weight,这个属性允许我们以比例的方式来指定控件的大小,在手机屏幕的适配方面起到了非常重要的作用。

修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity"

android:orientation="horizontal">

android:id="@+id/input_message"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

/>

android:id="@+id/send"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Send"/>

这里android:layout_width都指定成了0dp,那么文本编辑框和按钮还能显示出来吗?答案是肯定的,因为我们这里使用了android:layout_weight属性,这时的控件宽度将不再由android:layout_width决定,这里指定成android:layout_width="0dp" 是一种比较规范的写法。另外dp是Android中用来作为控件大小、间距等属性的单位。

我们这里EditText和Button元素中的 android:layout_weight 都指定为1,意思是EditText和Button平分屏幕的水平宽度。

控件所占的比例是这么算出来的:系统会将LinearLayout下的所有控件的 android:layout_weight 值相加得到一个总值,每个控件所占的比例就是该控件的android:layout_weight值除以这个总值得到的

我们还可以通过指定部分控件的android:layout_weight值来实现更好的效果,修改activity_main.xml中代码,如下所示

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"

tools:context=".MainActivity"

android:orientation="horizontal">

android:id="@+id/input_message"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:hint="Type something"

/>

android:id="@+id/send"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Send"/>

这里仅指定了EditText的android:layout_weight属性,Button的android:layout_width改为了wrap_content,这时Button的宽度仍然是按照wrap_content来计算,而EditText则会占满屏幕所剩余的空间。这种编程方式,不仅在各种屏幕的适配方面非常好,而且看起来也更加舒服。

重新运行程序,效果如下所示

image

相对布局

RelativeLayout又称相对布局,也是一种非常常用的布局。

我们直接来体验一下,修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity">

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="Button1"/>

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="Button2"/>

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="Button3"/>

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentBottom="true"

android:text="Button4"/>

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentBottom="true"

android:text="Button5"/>

以上代码不需要过多的解释,因为它们实在太好理解了。android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_alignParentRight、android:layout_centerInParent、android:layout_alignParentBottom这几个属性我们之前没有接触过,但是它们的名字已经完全说明了它们的作用。

重新运行程序,效果如图所示

image

上面的例子是控件相对于布局定位的,那么控件能否相对于控件进行定位呢?当然是可以的。下面我们就来实践一下,修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity">

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="Button3"/>

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button3"

android:layout_toLeftOf="@id/button3"

android:text="Button1"/>

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button3"

android:layout_toRightOf="@id/button3"

android:text="Button2"/>

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button3"

android:layout_toLeftOf="@id/button3"

android:text="Button4"/>

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button3"

android:layout_toRightOf="@id/button3"

android:text="Button5"/>

android:layout_above属性可以让一个控件位于另一个控件的上方,需要为这个属性指定相对控件id的引用,这里我们填入@id/button3,表示该控件位于Button3的上方。android:layout_below属性则是让一个控件位于另一个控件的下方。android:layout_toLeftOf 和 android:layout_toRightOf 则分别表示让一个控件位于另一个控件的左侧和右侧

当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,否则会出现找不到id的情况。重新运行程序,效果如图

image

帧布局

FrameLayout又称作帧布局,它比前面两种布局就简单太多了,应用场景也比较少。没有方便的定位方式,所有的控件默认放在布局的左上角。

下面我们通过例子来看看吧,修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity">

android:id="@+id/text_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is TextView"

/>

android:id="@+id/image_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher"

/>

FrameLayout中只放置了TextView和ImageView,需要注意的是,当前项目中我们并没有准备任何图片,所以这里ImageView直接使用了@mipmap来访问ic_launcher这张图,虽然这种用法的场景很少,但是却是完全可行的。重新运行程序,效果如图所示

可以看到,文字和图片都是位于布局的左上角,因为ImageView是在TextView之后添加的,所以图片压在了文字上面。我们可以通过 android:layout_gravity 来指定控件在布局中的对齐方式

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"

tools:context=".MainActivity">

android:id="@+id/text_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This is TextView"

android:layout_gravity="left"

/>

android:id="@+id/image_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher"

android:layout_gravity="right"

/>

我们指定了TextView左对齐,ImageView右对齐。重新运行程序,效果如下图所示

image

百分比布局

百分比布局可以直接指定控件在布局所占的百分比。由于LinearLayout本身已经支持按比例指定控件的大小了,因此百分比布局只对FrameLayout和RelativeLayout这两个布局进行了功能扩展,提供了 PercentFrameLayout 和 PercentRelativeLayout 这两个全新的布局

下面我们直接来实践一下吧,因为百分比布局属于新增布局,所以我们需要在项目的build.gradle中添加百分比布局库的依赖。

打开app/build.gradle文件,在dependencies闭包中添加如下内容:

implementation 'androidx.percentlayout:percentlayout:1.0.0'

需要注意的是,每当修改了任何的gradle文件,Android Studio都会有如下图所示的提示

image

这个提示告诉我们,gradle自上次同步以后又发生了变化,需要再次同步才能让项目正常工作。这里只需要点击Sync Now就可以了,然后gradle会开始同步,把我们新添加的百分比布局库引入到项目当中。

接下来修改activity_main.xml中的代码,如下所示

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"

tools:context=".MainActivity">

android:id="@+id/button1"

android:text="Button 1"

android:layout_gravity="left|top"

app:layout_widthPercent="50%"

app:layout_heightPercent="50%"

/>

android:id="@+id/button2"

android:text="Button 2"

android:layout_gravity="right|top"

app:layout_widthPercent="50%"

app:layout_heightPercent="50%"

/>

android:id="@+id/button3"

android:text="Button 3"

android:layout_gravity="left|bottom"

app:layout_widthPercent="50%"

app:layout_heightPercent="50%"

/>

android:id="@+id/button4"

android:text="Button 4"

android:layout_gravity="right|bottom"

app:layout_widthPercent="50%"

app:layout_heightPercent="50%"

/>

最外层我们使用了PercentFrameLayout,由于百分比布局并不是内置在系统SDK当中的,所示需要把完整的包路径写出来。然后必须定义一个app的命名空间,这样我们才能使用百分比布局的自定义属性。

使用 app:layout_widthPercent 属性将各按钮的宽度指定为布局的50%,使用 app:layout_heightPercent 属性将各按钮的高度指定为布局的50%。这里之所以能使用app前缀是因为我们刚才定义了app的命名空间,这跟我们一直能使用android前缀是一样的道理。

不过PercentFrameLayout还是继承了FrameLayout的特性,即所有的控件默认摆放在布局的左上角,为了让控件不重叠。我们这里借助了android:layout_gravity属性来分别把这4个按钮放在了左上,右上,左下,右下4个位置。

重新运行程序,效果如下图所示

image

PercentFrameLayout的用法就介绍到这里,另外一个 RelativeFrameLayout 的用法也是非常的相似,它继承了RelativeLayout的所有属性,也可以使用 app:layout_widthPercent 和 app:layout_heightPercent 来指定控件的宽高。相信聪明的你一定可以举一反三,这里就不再做详细的介绍了。当然Android的布局不止这些,由于应用的比较少,所以就不再一一举例了,如果你感兴趣的话可以自行去了解。

内容参考自《第一行代码》

公众号.png

android 垂直方向布局,Android详解4种基本布局相关推荐

  1. Android --- 屏幕方向screenOrientation属性详解

    landscape 限制界面为横屏,旋转屏幕也不会改变当前状态. portrait 限制界面为竖屏,旋转屏幕也不会改变当前状态 sensor 根据传感器定位方向,旋转手机90度,180,270,360 ...

  2. ANDROID L——Material Design详解(主题和布局)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  3. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高...

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  4. Android自定义View进阶-MotionEvent详解

    欢迎Follow我的GitHub, 关注我的CSDN. 其余参考Android目录 我们微信公众号:杨守乐 推荐文章: 如果你喜欢上了一个程序员小伙,献给所有的程序员女友 学习资料(干货汇集)不断更新 ...

  5. Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!...

    Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...

  6. 基于百度地图实现Android定位功能实现(详解+教程)

    基于百度地图实现Android定位功能实现(详解+教程) 1.初始化 (一)获取到SHA1 (1)找到View中的Tool Windows并打开Terminal (2)找到你的jdk的bin目录,小编 ...

  7. Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!

    Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...

  8. 《Android多媒体应用开发实战详解:图像、音频、视频、2D和3D》——2.3节简析Android安装文件...

    本节书摘来自异步社区<Android多媒体应用开发实战详解:图像.音频.视频.2D和3D>一书中的第2章,第2.3节简析Android安装文件,作者 王石磊 , 吴峥,更多章节内容可以访问 ...

  9. Android自定义控件系列八:详解onMeasure()(二)--利用onMeasure测量来实现图片拉伸永不变形,解决屏幕适配问题

    上一篇文章详细讲解了一下onMeasure/measure方法在Android自定义控件时的原理和作用,参看博文:Android自定义控件系列七:详解onMeasure()方法中如何测量一个控件尺寸( ...

  10. Android基础总结: Camera2详解之一 API学习

    Camera2的API出来有些年头了,只是赶项目多次使用,没时间好好总结,年终了,正好结合google的官方Camera2demo 和开发中使用的情况,做个详细梳理,研究总结之后,才发现Camera2 ...

最新文章

  1. 协议模型的最底层是_CAN通信协议栈(二) 之对ISO11898-1的理解
  2. response返回一段html,iframe调用后台方法通过response返回html代码
  3. 选择最大值的那条记录 sql
  4. 每个Java开发人员应拥有的持久断点
  5. 【springboot基础】配置日志输出级别以及输出位置
  6. os如何读取图片_CV:基于face库利用cv2调用摄像头根据人脸图片实现找人
  7. Python:PyCharm选择性忽略PEP8警告
  8. win7系统服务器管理器在哪里找,win7打开服务管理器
  9. 向日葵 远程开机 linux,教你使用向日葵开机棒轻松实现远程开机
  10. Javascript技巧大集合(转自http://www.mscto.com/JavaScript/041043806.html)
  11. 关闭阿里云的短信提醒
  12. not allowed to launch “localhost:81/XXXXX“
  13. linux 虚拟光驱软件,Ubuntu Linux下强大的虚拟光驱软件 :AcetoneISO
  14. Easy-Es核心功能深度介绍
  15. indoor是什么意思_indoor是什么意思
  16. AttributeError: ‘DatetimeProperties’ object has no attribute ‘weekday_name’ 的解决方法
  17. windows设置显示隐藏文件
  18. 2013年8月28日、PS初步入门|and|Maven了解
  19. php验证码大全(实例分享),php图片验证码的例子
  20. LayaAir引擎开发(基于JS开发)

热门文章

  1. QCC512x QCC302x Earbud 工程增加三击事件
  2. SpringBoot非官方教程 | 终章:文章汇总
  3. 医药领域知识图谱快速及医药问答项目
  4. STM32F205 STM32F207 STM32F215 STM332F217 用户手册,使用手册,编程手册三合一下载地址
  5. 100、新华三交换机配置方法及操作案例-弱电必备
  6. 蓝桥杯和noip都考C语言么,为什么NOIP信息学奥赛C++普及组师资匮乏
  7. 乾颐堂现任明教教主(2014年课程)TCPIP协议详解卷一 第三节课笔记
  8. (附源码)springboot校园购物网站 毕业设计 041037
  9. 智能控制在计算机领域的应用,智能控制的主要应用领域
  10. WPF实现鼠标拖动框选功能