Android layout属性之gravity和layout_gravity

摘要:
gravity是控制其内容或者包含的views在该view中的位置2.layout_gravity是表示该view在其父容器viewgroup中的位置。

1. gravity用来描述当前view的内容在view中的位置。

gravity是控制其内容或者包含的views在该view(或view group)中的位置

2. layout_gravity是表示该view在其父容器view group中的位置。

该属性只在父容器是LinearLayout和FrameLayout时有效

gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置

android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧。

android:layout_gravity:是相对于包含改元素的父元素来说的,设置该元素在父元素的什么位置

比如TextView: android:layout_gravity表示TextView在界面上的位置,android:gravity表示TextView文本在TextView的什么位置,默认值是左侧.

基本选项值:

ValueDescription
topPut the object at the top of its container, not changing its size. 将对象放在其容器的顶部,不改变其大小.
bottomPut the object at the bottom of its container, not changing its size. 将对象放在其容器的底部,不改变其大小.
leftPut the object at the left edge of its container, not changing its size. 将对象放在其容器的左侧,不改变其大小.
rightPut the object at the right edge of its container, not changing its size. 将对象放在其容器的右侧,不改变其大小.
center_verticalPlace object in the vertical center of its container, not changing its size. 将对象纵向居中,不改变其大小. 垂直对齐方式:垂直方向上居中对齐。
fill_verticalGrow the vertical size of the object if needed so it completely fills its container. 必要的时候增加对象的纵向大小,以完全充满其容器. 垂直方向填充
center_horizontalPlace object in the horizontal center of its container, not changing its size. 将对象横向居中,不改变其大小. 水平对齐方式:水平方向上居中对齐
fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container. 必要的时候增加对象的横向大小,以完全充满其容器. 水平方向填充
centerPlace the object in the center of its container in both the vertical and horizontal axis, not changing its size. 将对象横纵居中,不改变其大小.
fillGrow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default. 必要的时候增加对象的横纵向大小,以完全充满其容器.
clip_verticalAdditional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges.

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

垂直方向裁剪

clip_horizontalAdditional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges.

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

水平方向裁剪

如下例子

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="100dip"
  9. android:layout_height="100dip"
  10. android:layout_gravity="bottom|center_horizontal"
  11. android:gravity="center|bottom"
  12. android:background="#00FF00"
  13. android:text="@string/textview"
  14. />
  15. <Button
  16. android:layout_width="100dip"
  17. android:layout_height="100dip"
  18. android:layout_gravity="bottom|left"
  19. android:gravity="left|top"
  20. android:background="#FF0000"
  21. android:text="@string/button"
  22. />
  23. </LinearLayout>

Android layout属性之gravity和layout_gravity第1张

注意,TextView并没有按照我们设置的android:layout_gravity属性那样显示在界面的下方正中央,Button也没有显示在界面的左下方。这是因为我们设置了 LinearLayout的android:orientation属性为"vertical"。对于LinearLayout如果设置 android:orientation="vertical",那么android:layout_gravity的设置只在水平方向生效,如上图 TextView显示在屏幕的水平正中央,而Button显示在水平方向的最左边;如果设置 android:orientation="horizontal",那么android:layout_gravity属性只在垂直方向生效

免责声明:文章转载自《Android layout属性之gravity和layout_gravity》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DOM的学习Ubuntu 10.04 右上角网络管理图标消失的解决的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

k8s健康检查(七)

默认的健康检查 强大的自愈能力是 Kubernetes 这类容器编排引擎的一个重要特性。自愈的默认实现方式是自动重启发生故障的容器。除此之外,用户还可以利用 Liveness 和 Readiness 探测机制设置更精细的健康检查,进而实现如下需求: 零停机部署。 避免部署无效的镜像。 更加安全的滚动升级。 每个容器启动时都会执行一个进程,此进程由 Do...

React-Native性能优化点

shouldComponentUpdate 确保组件在渲染之后不需要再更新的,即静态组件,尽量在其中增加shouldComponentUpdate方法,防止二次消耗所产生的性能消耗 shouldComponentUpdate() { //完全静态的组件,无需更新 return false; } key key是react的一个特殊的属性...

【华为云技术分享】容器与虚拟化的结合:浅谈“安全容器”技术发展趋势

摘要:无论公有云还是私有云厂商,都认识到了将虚拟化的隔离性和容器的高效运维特性相结合,是云原生平台发展的必然趋势。 容器是如何解决隔离问题的 众所周知,容器技术的出现有两个关键原因: 1.  软件运行过程中的资源和环境的隔离。 2.  软件因为运行环境多样带来的打包和配置的复杂性。 而对于软件运行环境的隔离需求,从计算机出现之初就已经开始了,多任务分时操作...

[转]html风格tooltip效果的实现

网页上的图片如果设置了alt属性,当鼠标移经时就会有tooltip出现,但是只能显示一行文本,有时需要多行文本,乃至图片来显示图片、链接或者其它HTML元素的提示信息时,alt属性就无能为力了。在本文提供了一种实现方案:为图片、链接等任意需要tooltip的HTML元素加上一个自定义的属性——"tooltip",tooltip的值即为要显示的提示信息,可以...

容器网络(二)docker容器访问外部网络及对外提供服务

由于docker容器访问外部网络及对外提供服务都使用到iptable,我们先了解下iptable的基础知识。 一、Iptables 1、iptables的链 iptables有5条默认的链,分别为: INPUT OUTPUT PREROUTING FORWARD POSTROUTING 2、iptables的表 iptables有4张表,分别为: f...

docker部署ASP.NET Core、Nginx、MySQL

2019/10/24, docker 19.03.4, ASP.NET Core 3.0, CentOS 7.6 摘要:ASP.NET Core 3.0 网站项目容器化部署,使用docker-compose编排Nginx容器、MySQL容器、web容器案例代码 依赖结构介绍 整个网站项目取名samplems,一共需要三个容器(按依赖顺序): 1.MySQL...