登陆 | 注册 设为首页 | 加入收藏 | 联系我们
太和养老网
热词老年艺术  助老机构  养老系统  

中心区域北京 天津 河北 山西 内蒙古 辽宁 吉林 黑龙江 上海 江苏 浙江 安徽 福建 江西 山东 河南 湖北 湖南 广东 广西 海南 重庆 四川 贵州 云南 西藏 陕西 甘肃 青海 宁夏 新疆 香港 澳门 台湾 全国城市养老院目录 全国县市养老院汇总目录 太和AI作品展 太和养老艺术网AI作品展示

Android五大布局详解——LinearLayout(线性布局)-飞

 

2023/7/6 17:12:18 ('互联网')

Android五大布局

本篇开始介绍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依次排列。


1.png

接下来将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依次排列。


2.png

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>

效果如图:


3.png

同样,倘若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>

效果如图:


4.png

下面来看两个长得很像的属性: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 举报,一经查实,本站将立刻删除。



扫码加微信详细咨询太和智慧养老产品和平台服务!

 

养老资讯
助老机构介绍
评论
已有 0 条评论

最新评论

推荐养老院

您希望养老院位于
  • 不限
  • 东城
  • 西城
  • 崇文
  • 宣武
  • 朝阳
  • 丰台
  • 石景山
  • 海淀
  • 门头沟
  • 房山
  • 通州
  • 顺义
  • 昌平
  • 大兴
  • 怀柔
  • 平谷
  • 延庆
  • 密云
您希望的价格范围
  • 不限
  • 500以下
  • 500-1000
  • 1000-2000
  • 2000-3000
  • 3000-5000
  • 5000以上
老人的情况是
  • 不限
  • 自理
  • 半自理
  • 全护理
  • 特护

姓名

年龄

电话

全国城市养老院



关于我们 | 联系方式 | 网站地图 | 友情链接

Copyright 2010-2022 京ICP备18035644号-3 北京太和 版权所有