android五大布局之线性布局。

  1.线性布局的特点:各个子元素彼此连接,中间不留空白

  

  而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用

  用的比较多的就是LinearLayout的weight(权重属性),下面写个案例来表明

  

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_weight=""
  11. android:background="#FFC90E"
  12. >
  13.  
  14. </LinearLayout>
  15.  
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight=""
  20. android:orientation="vertical"
  21. android:background="#DA70D6"
  22. >
  23.  
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="" >
  28. </LinearLayout>
  29.  
  30. <LinearLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_weight=""
  34. android:background="#66CDAA"
  35. android:orientation="horizontal"
  36. >
  37.  
  38. <TextView
  39. android:id="@+id/txt1"
  40. android:layout_width="wrap_content"
  41. android:layout_height="match_parent"
  42. android:background="#FFF663"
  43. android:layout_weight=""
  44.  
  45. />
  46. <TextView
  47. android:id="@+id/txt2"
  48. android:layout_width="wrap_content"
  49. android:layout_height="match_parent"
  50. android:background="#E82917"
  51. android:layout_weight=""
  52.  
  53. />
  54. <TextView
  55. android:id="@+id/txt3"
  56. android:layout_width="wrap_content"
  57. android:layout_height="match_parent"
  58. android:background="#FFF663"
  59. android:layout_weight=""
  60.  
  61. />
  62.  
  63. </LinearLayout>
  64.  
  65. </LinearLayout>
  66. </LinearLayout>

效果图为:

android:layout_gravity 本元素相对于父元素的重力方向

android:gravity 本元素所有子元素的重力方向

android:orientation 线性布局以列或行来显示内部子元素

android:layout_weight 子元素对未占用空间水平或垂直分配权重值

当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。!!!!

当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。!!!!

android:layout_gravity 和 android:gravity 的区别

android:gravity对元素本身起作用-本身元素显示在什么位置

android:layout_gravity相对与它的父元素-元素显示在父元素的什么位置。

如:Button控件

android:layout_gravity 表示button在界面上的位置

android:gravity表示button上的字在button上的位置。

可选值[多选时用“|”分开]

top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

top 将对象放在其容器的顶部,不改变其大小.

bottom 将对象放在其容器的底部,不改变其大小.

left将对象放在其容器的左侧,不改变其大小.

right将对象放在其容器的右侧,不改变其大小.

center_vertical 将对象纵向居中,不改变其大小.

垂直对齐方式:垂直方向上居中对齐。

fill_vertical 必要的时候增加对象的纵向大小,以完全充满其容器. 垂直方向填充

center_horizontal 将对象横向居中,不改变其大小水平对齐方式:水平方向上居中对齐

fill_horizontal 必要的时候增加对象的横向大小,以完全充满其容器. 水平方向填充

center 将对象横纵居中,不改变其大小.

fill 必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical 附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪

clip_horizontal 附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

例子

TextView要让文本垂直/水平居中显示,有两种情况需要考虑:

1、layout_width/layout_height为wrap_content,此时要让TextView在父控件上居中显示,必须设置layout_gravity=”center”。

2、layout_width/layout_height为fill_parent,此时由于TextView已占据父窗体所有空间,必须设置gravity=”center”。

android—-线性布局的更多相关文章

  1. Android线性布局(Linear Layout)

    Android线性布局(Linear Layout) LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,垂直或是水平方向.我们能够用androi ...

  2. Android 线性布局(LinearLayout)相关官方文档 - 指南部分

    Android 线性布局(LinearLayout)相关官方文档 - 指南部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用 ...

  3. Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分

    Android 线性布局(LinearLayout)相关官方文档 - 布局參数部分 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  4. Android线性布局

    线性布局 LinearLayout 是一个视图组,用于使所有子视图在单个方向(垂直或水平)保持对齐. 您可以使用 android:orientation 属性指定布局方向. LinearLayout ...

  5. android 线性布局

    activity_main.xml线性布局 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  6. Android线性布局和帧布局

    第二次,本牛崽十分从容,今天咱们来讲讲Android Q之布局,我遇到的问题与自己学到的,大牛不要嘲笑哈,有错误可以指出来,本牛崽看到就改了. 今天我的学长跟我们开始了布局,布局看资料说好像有5种,又 ...

  7. [android] 线性布局和布局的组合

    /****************2016年4月25日 更新******************************/ 知乎:对于开发者来说,Android 的开发者选项里有哪些实用的功能? 汤涛 ...

  8. Android 线性布局 LinearLayout

    垂直布局 vertical <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  9. iOS - starckView 类似Android线性布局

    同iOS以往每个迭代一样,iOS 9带来了很多新特性.UIKit框架每个版本都在改变,而在iOS 9比较特别的是UIStackView,它将从根本上改变开发者在iOS上创建用户界面的方式.本文将带你学 ...

随机推荐

  1. UI第八节——UISegmentedControl

    - (void)viewDidLoad {    [super viewDidLoad];    NSArray *items = @[@"消息", @"电话" ...

  2. PHP realpath() 函数

    定义和用法 realpath() 函数返回绝对路径. 该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),返回绝对路径名. 若失败,则返回 false.比如说文件不存在的话 ...

  3. redis部署

    下载软件 [root@localhost /]# wget http://download.redis.io/releases/redis-2.8.9.tar.gz 解压.编译.安装 [root@lo ...

  4. Java Native Interfce三在JNI中使用Java类的普通方法与变量

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 前面我们学习了如何在JNI中通过参数来使用J ...

  5. 下拉列表 select-option ; select-optgroup-option

    HTML中的下拉列表: <select> <option value ="1">Volvo</option> <option value  ...

  6. HikariCP

    HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他连接池. 为何要使用HiKariCP?这要先从BoneCP说起: 什么?不是有C3P0/DBCP这些成熟的数据库连接池吗 ...

  7. Java批量文件打包下载

    经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名, ...

  8. MySql: 忘记root密码

    win7 + mysql 5.6.35 C:\Windows\system32>mysql --versionmysql Ver 14.14 Distrib 5.6.35, for Win64 ...

  9. Angularjs 双向绑定机制解析

    文章转自:http://www.2cto.com/kf/201408/327594.html AngularJs 的元素与模型双向绑定依赖于循环检测它们之间的值,这种做法叫做脏检测,这几天研究了一下其 ...

  10. Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)

    在IDEA中将项目部署到本地Tomcat下进行运行并验证整合结果 (1).点击如下图所示的下拉按钮,弹出Edit Configurations...后点击该项. (2).跳出如下界面后,点击红框内的& ...