本篇开始介绍Android的五大布局的知识,一个丰富的界面显示总是要有众多的控件来组成的,那么怎样才能让这些控件能够按你的想法进行摆放,从而自定义你所想要的用户界面呢?这就牵涉到本章将要学习的知识————五大布局。本篇将依次对LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)、GridLayout(网格布局)进行介绍。
LinearLayout(线性布局)这是一个非常常用的布局,它会将其中的控件在线性方向上依次排列,通过android:orientation属性指定其控件的排列方向,有vertical(垂直方向)以及horizontal(水平方向)排列。新建UILayoutTsetOne项目,其他设置保持默认。修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" /></LinearLayout>
模拟器中运行结果如下图所示,从图中可以看出,定义的三个button控件按照vertical依次排列。
接下来将vertical参数改变为horizontal参数。
<?xml version="1.0" encoding="utf-8"?(龙吸水是什么意思?中国跳高名将张国伟退役后经常在社交媒体上表演花式喝可乐,这个“绝活”被称为龙吸水。网友调侃张国伟是国家一级龙吸水运动员,也有网友称赞张国伟是龙吸水第一人。)><LinearLayout xmlns:andro android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" /></LinearLayout>
运行程序,效果如下,从图中可以看出,定义的三个button组件按照horizontal依次排列。
attention!
倘若LinearLayout的排列方向指定为horizontal,则内部的控件就绝对不能将宽度指定为match_parent,因为如果这样设置,单独的控件将会将整个水平方向占满,其他控件将没有放置的位置了。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" /></LinearLayout>
效果如图:
同样,倘若LinearLayout的排列方向指定为vertical,则内部的控件就绝对不能将高度指定为match_parent。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" /></LinearLayout>
效果如图:
下面来看两个长得很像的属性:android:gravity属性和android:layout_gravity属性。
android:gravity属性:用于指定文字在控件中的对齐方式。可以选择的值有:top、bottom、left、right、center等,还可以用“|”来同时指定多个值,其中center值将相当于center_vertical|center_horizontal,表示文字在垂直和水平方向都居中对齐。android:layout_gravity属性:用于指定控件在布局中的对齐方式。其可选值和android:gravity属性差不多,需要注意的是,当LinearLayout的排列方向是horizontal时只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样,当LinearLayout的排列方向是vertical时,只有水平方向上的对齐方式才会生效。修改activity_main.xml中的代码:<?xml version="1.0" encodi
扫码加微信详细咨询太和智慧养老产品和平台服务!
版权声明:
---------------------------------------------------------------
所有信息来源于互联网,本文的版权归原作者所有,不代表本网观点和立场。
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 81480447@qq.com 举报,一经查实,本站将立刻删除。
扫码加微信详细咨询太和智慧养老产品和平台服务!
养老资讯
- 我们经常能大街上看到很多
- 上联:残阳如血,殷红半边
- 怎样的职业最容易"阅人无
- 标本兼治是什么意思(面部
- 用想象力给“记忆”打比喻
- 家庭生活压抑憋屈的心情说
- 化作春泥更护花的上一句(
- 供应商供物料经常不及时,
- 博览群书的意思是什么(至
- 孔夫子搬家歇后语是什么(
- 《世说新语》是什么内容?
- 情人眼里出西施(我国有四
- 你曾经醉生梦死过吗?有些
- 古代文人为什么努力读《左
- 如何评价电影《日暮》?周
- 善良名义的放生,你怎么看
- 惠风和畅书法作品(有人觉
- 三尺青锋怀天下(有哪些气
- 刘盈有竞争对手,为何太子
- 你认为许嵩最悲情的歌词是
- 飘飘然的意思(飘飘然的几
- 世风日下图片(现代人都喜
- 暮气沉沉的意思是什么(什
- 汉武托孤打是什么动物(汉
- 生活中哪些事情会让你很恼
- 毛骨悚然电影(2020年
- 草芥人命和草菅人命(《我
- 刘禹锡和柳宗元是如何成为
- 侯府长媳吱吱(你身边那些
- 电影《绝密飞行》中的可变
- 结论的近义词(表示结论的
- 循规蹈矩的生活(每天循规
- 于心何忍还是余心何忍(到
- 为什么拿破仑攻打俄罗斯时
- 自食其果是什么意思?南部
- 龙凤呈祥图片(古人说过“
- 天子门生电影(农村俗话给
- 有的老年人辛苦把儿女养大
- 几月份去九寨沟最好应该去
- 女孩落落大方最有气质(有
助老机构介绍
姓名:
年龄:
电话: