安卓:gravity 和layout_gravity 的关系

一、两者区别

android:gravity 与 android:layout_gravity 的区别在于:

gravity用于设置内部内容位置,而layout_gravity 用于设置当前组件与父组件的位置


二、android:gravity研究

一开始,我以为这个是属于布局管理的XML属性,于是查看并搜索了FrameLayout以及它继承的View类,ViewGroup类结果是:没有找到这个XML属性的影子,于是回想起来,好像在Button中用到过,但搜索其Button却没有发现,但是通过类继承图


我肯定了这个属性一定是属于TextView的,结果果然在其中找到了它。

其实这就说明了,gravity一定是要有内容才有意义的,所以它才会出现在这个位置。

而对于TextView而言,它实际是非常多含有【内容】的组件父类。

例如:

ButtonCheckedTextViewChronometerDigitalClockEditTextTextClock

按钮, 检查文本框,计时器,数字钟,编辑框,文字时钟


三、android: layout_gravity

这个XML属性,就更有意思了,

它既不在View中,也不在各个布局中,例如:

LinearLayout 线性布局

FrameLayout 堆栈布局(也有叫做帧布局或者框架布局的)

GridLayout  节点布局

而是在以上三种布局的内部类中:

这说明了至少两个问题:

(1) 某些布局并不需要此属性,如绝对布局AbsoluteLayout,相对布局RelativeLayout,所以他们的 布局参数中,并不包含此layout_gravity属性

(2) 此属性的实现,也是根据具体需求来实现的,即不同的布局,对于它的实现是不一样的。

附带一张布局常量表:

Constant  Value Description
top 0x30 Push object to the top of its container, not changing its size.
将对象挤压到容器顶部,而不改变它的尺寸
bottom 0x50 Push object to the bottom of its container, not changing its size.
将对象挤压到容器底部,而不改变它的尺寸
left 0x03 Push object to the left of its container, not changing its size.
将对象挤压到容器左边,而不改变它的尺寸
right 0x05 Push object to the right of its container, not changing its size.
将对象挤压到容器右边,而不改变它的尺寸
center_vertical 0x10 Place object in the vertical center of its container, not changing its size.
在垂直方向上,将对象居中,而不改变它的尺寸
fill_vertical 0x70 Grow the vertical size of the object if needed so it completely fills its container.
如果有必要的话,增加垂直方向的尺寸使其能够完整的填充装载它的容器。
center_horizontal 0x01 Place object in the horizontal center of its container, not changing its size.
在水平方向上,将对象居中,而不改变它的尺寸
fill_horizontal 0x07 Grow the horizontal size of the object if needed so it completely fills its container.
如果有必要的话,增加水平方向的尺寸使其能够完整的填充装载它的容器。
center 0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
对象在水平和垂直方向上居中,而不改变尺寸。
fill 0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
对象在水平方向和垂直方向上都填充容器
clip_vertical 0x80 Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds.
The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge,
and neither will clip both edges.
额外的选项,设置是否可剪切边缘。
这是基于垂直布局的,顶部对齐时,剪切底部;底部对齐时剪切顶部;
除此之外剪切顶部和底部.
clip_horizontal 0x08 Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds.
The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge,
and neither will clip both edges.
与上面类型,不过是左边对其剪裁右边,右边对其剪裁左边...
start 0x00800003 Push object to the beginning of its container, not changing its size.
将对象挤压如容器开始的位置,而不改变它的尺寸
end 0x00800005 Push object to the end of its container, not changing its size.
将对象放置在容器结束的位置,而不改变它的尺寸


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值