线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是

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

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

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

android:layout_weight (线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。

前提是子元素 设置了 android:layout_width = "fill_parent" 属性(水平方向)

或 android:layout_height = "fill_parent" 属性(垂直方向)

如果某个子元素的 android:layout_width = "wrap_content"

          android:layout_height =" wrap_content”

则 android:layout_weight 的设置值 对该方向上空间的分配刚好相反。

下面以一个简单例子来说明这 4个参数

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4
 5                            android:layout_height = "200dp"
 6
 7                            android:layout_width = "200dp"
 8
 9                            android:background = "#AABBCC"
10
11                            android:orientation= "horizontal"
12
13                            android:layout_gravity= "center" >
14
15                            < TextView android:text = "ONE"
16
17                                                android:background = "#aa0000"
18
19                                                android:layout_height = "wrap_content"
20
21                                                android:layout_width = "wrap_content"
22
23                                                android:layout_margin = "1dp" />
24
25                             < TextView android:text = "TWO"
26
27                                                android:background = "#aa0000"
28
29                                                android:layout_height = "wrap_content"
30
31                                                android:layout_width = "wrap_content"
32
33                                                android:layout_margin = "1dp" />
34
35 </ LinearLayout >

说明:在上面的例子中,根布局是LinearLayout, 其包含有2 个TextView 视图,为了对参数 android:layout_gravity有直观的了解,对根布局 LinearLayout 特意加了 3 个参数

android:layout_height = "200dp"

android:layout_width   = "200dp"

android:background     = "#AABBCC"

为布局指定了固定的宽度和高度,以及背景颜色,上面的例子运行后效果如下图:

说明:对LinearLayout 中的参数android:layout_gravity 来说,其意义是指定本布局相对于父布局的重力方向,由于该布局的已经是根布局,其父布局是整个屏幕,那么该参数设置的是相对于屏幕的位置,可以换不同的参数top|bottom|left|right 等等参数来试验。

现在增加参数 android:gravity = "bottom|right" 完整 XML 如下,看看效果

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4
 5                       android:layout_height = "200dp"
 6
 7                       android:layout_width = "200dp"
 8
 9                       android:background = "#AABBCC"
10
11                       android:orientation="horizontal"
12
13                       android:layout_gravity= "center"
14
15                       android:gravity = "bottom|right " >
16
17                       < TextView android:text = "ONE"
18
19                                      android:background = "#aa0000"
20
21                                      android:layout_height = "wrap_content"
22
23                                      android:layout_width = "wrap_content"
24
25                                      android:layout_margin = "1dp" />
26
27                       < TextView android:text = "TWO"
28
29                                      android:background = "#aa0000"
30
31                                      android:layout_height = "wrap_content"
32
33                                      android:layout_width = "wrap_content"
34
35                                      android:layout_margin = "1dp" />
36
37 </ LinearLayout >

通过改变android:gravity 参数的值可以看到实际效果。

参数 android:orientation= " horizontal " 决定了每个子元素各占一列,如果

参数 android:orientation= " vertical " , 则每个子元素各占一行,也就是从上到下排列了。

对于 LinearLayout 布局的子元素,给每个子元素加上参数 android:layout_weight

看看效果

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4
 5                       android:layout_height = "200dp"
 6
 7                       android:layout_width = "200dp"
 8
 9                       android:background = "#AABBCC"
10
11                       android:layout_gravity = "center"
12
13                       android:gravity = "bottom|right"
14
15                       android:orientation = "horizontal" >
16
17                       < TextView android:text = "ONE"
18
19                                      android:background = "#aa0000"
20
21                                      android:layout_height = "wrap_content"
22
23                                      android:layout_width = "wrap_content"
24
25                                      android:layout_margin = "1dp"
26
27                                      android:layout_weight = "1" />
28
29                       < TextView android:text = "TWO"
30
31                                      android:background = "#aa0000"
32
33                                      android:layout_height = "wrap_content"
34
35                                      android:layout_width = "wrap_content"
36
37                                      android:layout_margin = "1dp"
38
39                                      android:layout_weight = "2" />
40
41 </ LinearLayout >

Text 为ONE 的权重为1 ,但明显占的宽度比TWO 的小,百思不得其解,后来得知,如果把TextView 的参数android:layout_width = "wrap_content" 全部修改为 android:layout_width = "fill_parent", 则 ok ,代码如下

 1 <? xml version = "1.0" encoding = "utf-8" ?>
 2
 3 < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 4
 5                       android:layout_height = "200dp"
 6
 7                       android:layout_width = "200dp"
 8
 9                       android:background = "#AABBCC"
10
11                       android:layout_gravity = "center"
12
13                       android:gravity = "bottom|right"
14
15                       android:orientation = "horizontal" >
16
17                       < TextView android:text = "ONE"
18
19                                      android:background = "#aa0000"
20
21                                      android:layout_height = "wrap_content"
22
23                                      android:layout_width = " fill_parent "
24
25                                      android:layout_margin = "1dp"
26
27                                      android:layout_weight = "1" />
28
29                       < TextView android:text = "TWO"
30
31                                      android:background = "#aa0000"
32
33                                      android:layout_height = "wrap_content"
34
35                                      android:layout_width = " fill_parent "
36
37                                      android:layout_margin = "1dp"
38
39                                      android:layout_weight = "2" />
40
41 </ LinearLayout >

转载于:https://www.cnblogs.com/laxlerbo/p/3837560.html

android 布局中 layout_gravity、gravity、orientation、layout_weight相关推荐

  1. 详解Android布局中gravity与layout_gravity属性

    在android布局中,我们经常会用到"重心"-gravity这个属性.但是gravity有不同的类型: gravity layout_gravity 相对布局中的layout_c ...

  2. android布局中使用include及需注意点

    在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题:一.使用include引入如现有标题栏布局block_header.x ...

  3. android 布局 站位符,基于android布局中的常用占位符介绍

    大家在做布局文件是肯定会遇到过下面的这种情况 填充出现问题,所以需要用到占位符规范填充 汉字常用占位符: android:layout_width="wrap_content" a ...

  4. Android布局中的空格以及占一个汉字宽度的空格的实现

    在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格:  一个汉字宽度的空格:   [用两个空格(  )占一个汉字的宽度时,两个空格比一 ...

  5. android 多个占位符,基于android布局中的常用占位符介绍

    大家在做布局文件是肯定会遇到过下面的这种情况 填充出现问题,所以需要用到占位符规范填充 汉字常用占位符: android:layout_width="wrap_content" a ...

  6. Android布局中margin与padding的区别

    我们知道Android开发不仅仅有代码的动态控制,而且还涉及到布局的静态搭建(xml).几乎在每一个xml文件中,我们总会看到margin和padding这两个属性,今天让我们初步探究一下它们之间的区 ...

  7. 从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight

    线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...

  8. Android布局中gravity、layout_gravity与layout_centerHorizontal属性区别介绍

    gravity使用区分 gravity:控制控件内文字的对齐方式,LinearLayout.RelativeLayout均生效 gravity是设置自身内部元素的对齐方式.比如一个TextView,则 ...

  9. Android Studio中layout_gravity与gravity

    Android Studio的视图有layout_gravity属性和gravity属性.其中gravity是"重力"的意思,在此引申为与力有关的"对齐方式". ...

最新文章

  1. Thread start()方法和run()方法的区别
  2. kubeadm安装高可用kubernetes v1.14.1
  3. Openstack部署工具
  4. python语音引擎深度学习_python深度学习之语音识别(speech recognize)
  5. python输出布尔值true_关于python中bool类型的重要细节
  6. ogg oracle 测试kafka_云MSP技本功|基于OGG 实现Oracle到Kafka增量数据实时同步
  7. 无人机如何通过GB28181协议接入视频会议
  8. DataParallel使用
  9. javascript函数传参
  10. 提交网址到bing搜索引擎
  11. 单片机_rom_ram,程序容量之迷
  12. msxml3.dll 错误 '800c0008'
  13. 程序员过中秋 | 如何用代码绘制月亮?
  14. 静态路由 动态路由 php,静态路由汇总(路由聚合)
  15. 矢量点积与矢量叉乘的微分
  16. MySQL出现Access denied for user ‘xxx‘@‘%‘ to database ‘xxxx‘问题
  17. Matplotlib 绘制柱状图 - 电影票房
  18. c语言写学程序,入门:我的第一个程序|学编程写游戏(C语言)
  19. 野火stm32开发板给定一个脉冲程序_STM32开发板哪个好,推荐一款高性价比stm32MP157开发板...
  20. 用两步排除法快速解答浮点数表示范围选择题

热门文章

  1. 产品网络推广方案之网站导航栏如何设置会更利于排名提升?
  2. 杭州网络推广浅谈细节优化之栏目页如何优化?
  3. 网络营销专员表示网络营销中设置不当会影响蜘蛛爬虫对网站抓取
  4. 软件测试方法单元测试例子,service单元测试例子
  5. 按键精灵脚本 php,HTML_按键精灵 脚本-学习VBS的一个不错的教程,今天我就从总体上对VBS进行介 - phpStudy...
  6. java object save,通过ObjectOutputStream发送文件,然后将其保存在Java中?
  7. :x 和 :wq 的区别
  8. Android - could not install *smartsocket* listener
  9. SIMD指令集——一条指令操作多个数,SSE,AVX都是,例如:乘累加,Shuffle等
  10. 动态DNS——本质上是IP变化,将任意变换的IP地址绑定给一个固定的二级域名。不管这个线路的IP地址怎样变化,因特网用户还是可以使用这个固定的域名 这样看的话,p2p可以用哇...