android中gravity什么意思,详解介绍android:layout_gravity 和 android:gravity 之间的区别

android开发必遇问题,最有可能忘记两者之间的区别的问题之一

如下是google搜索出来的结果

cedd1d147a2c241b6e0e103b6708e0a2.png

记忆方法

联想/形像記法喎? f/ware/vc/"="" target="_blank" class="keylink">vcd4ncjxwpiogwpvtw7pj1lgx5mg/tctk9nduvmfcvkosz3jhdml0ecrhwoc1xlpj1lgx5mg/o6zx1mi7vs3kx7/y1sbx073ateo1xmxfsobqp7n7o6zj6nbd19s8ustasr+1xmxfsobw2ndeo6y8tmxfsobe2rk/19pa4lxess7k/agjphn0cm9uzz7fxbdmy/ziy7xe1tjqxcjw97avktwvc3ryb25npjwvcd4ncjxwpiogbgf5b3v0x2dyyxzpdhm1xmew17rt0gxhew91dkosvltk9npatgf5b3v0ugfyyw1zwoc1xmr00ntwtaosylu680xhew91dfbhcmftc8rhupi4uhzpzxftw7xeo6y8tcbsyxlvdxrfz3jhdml0ecrhyejww9fuvlru2ri4dmlld7xexcww5tbyteojrly0sbvfxbdmyrg1xlloyv2jotxzdhjvbmc+sbvl/mjlxcww5rxe1tjqxkoosbu2r6oppc9zdhjvbmc+pc9wpg0kpggxps7etbxltcp3pc9omt4ncjxomj5hbmryb2lkomdyyxzpdhk8l2gypg0kpha+pgltzybhbhq9"这里写图片描述" src="/uploadfile/collfiles/20180606/2018060608595671.png" title="\" />

关键字 should position , 主动

android:layout_gravity

464e0c742dfd05eef7e7eaba916146c5.png

关键字 should be placed, 被动

源码左证

如下是linearlayout的onlayout函数中可以看到两个参数的使用情况

void layoutvertical(int left, int top, int right, int bottom) {

final int paddingleft = mpaddingleft;

int childtop;

int childleft;

// where right end of child should go

final int width = right - left;

int childright = width - mpaddingright;

// space available for child

int childspace = width - paddingleft - mpaddingright;

final int count = getvirtualchildcount();

final int majorgravity = mgravity & gravity.vertical_gravity_mask;

final int minorgravity = mgravity & gravity.relative_horizontal_gravity_mask;

// 使用mgravity来计算第一个子view的top

switch (majorgravity) {

case gravity.bottom:

// mtotallength contains the padding already

childtop = mpaddingtop + bottom - top - mtotallength;

break;

// mtotallength contains the padding already

case gravity.center_vertical:

childtop = mpaddingtop + (bottom - top - mtotallength) / 2;

break;

case gravity.top:

default:

childtop = mpaddingtop;

break;

}

for (int i = 0; i < count; i++) {

final view child = getvirtualchildat(i);

if (child == null) {

childtop += measurenullchild(i);

} else if (child.getvisibility() != gone) {

final int childwidth = child.getmeasuredwidth();

final int childheight = child.getmeasuredheight();

//在排子view时才使用到子view的layoutparams中的gravity

final linearlayout.layoutparams lp =

(linearlayout.layoutparams) child.getlayoutparams();

int gravity = lp.gravity;

if (gravity < 0) {

gravity = minorgravity;

}

相关补充

viewgroup是个抽象类,子类继承它时需要override onlayout方法 linearlayout、relativelayout等viewgroup子类就分别实现了自己的排版算法(override onlayout方法) viewgroup子类们在排版过程中使用到gravity与layout_gravity等参数来排版子view(内容),值得注意的是mgravity不是viewgroup的成员变量,另外各子类的排版策略是不一样的,所以gravity不是必须存在的与使用的,如framelayout

如下是framelayout的onlayout方法的代码

protected void onlayout(boolean changed, int left, int top, int right, int bottom) {

layoutchildren(left, top, right, bottom, false /* no force left gravity */);

}

void layoutchildren(int left, int top, int right, int bottom,

boolean forceleftgravity) {

final int count = getchildcount();

final int parentleft = getpaddingleftwithforeground();

final int parentright = right - left - getpaddingrightwithforeground();

final int parenttop = getpaddingtopwithforeground();

final int parentbottom = bottom - top - getpaddingbottomwithforeground();

for (int i = 0; i < count; i++) {

final view child = getchildat(i);

if (child.getvisibility() != gone) {

final layoutparams lp = (layoutparams) child.getlayoutparams();

final int width = child.getmeasuredwidth();

final int height = child.getmeasuredheight();

int childleft;

int childtop;

int gravity = lp.gravity;

if (gravity == -1) {

gravity = default_child_gravity;

}

final int layoutdirection = getlayoutdirection();

final int absolutegravity = gravity.getabsolutegravity(gravity, layoutdirection);

final int verticalgravity = gravity & gravity.vertical_gravity_mask;

switch (absolutegravity & gravity.horizontal_gravity_mask) {

case gravity.center_horizontal:

childleft = parentleft + (parentright - parentleft - width) / 2 +

lp.leftmargin - lp.rightmargin;

break;

case gravity.right:

if (!forceleftgravity) {

childleft = parentright - width - lp.rightmargin;

break;

}

case gravity.left:

default:

childleft = parentleft + lp.leftmargin;

}

switch (verticalgravity) {

case gravity.top:

childtop = parenttop + lp.topmargin;

break;

case gravity.center_vertical:

childtop = parenttop + (parentbottom - parenttop - height) / 2 +

lp.topmargin - lp.bottommargin;

break;

case gravity.bottom:

childtop = parentbottom - height - lp.bottommargin;

break;

default:

childtop = parenttop + lp.topmargin;

}

child.layout(childleft, childtop, childleft + width, childtop + height);

}

}

}

喎?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值