欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解介绍android:layout_gravity 和 android:gravity 之间的区别

程序员文章站 2022-12-11 07:51:22
android开发必遇问题,最有可能忘记两者之间的区别的问题之一 如下是google搜索出来的结果 记忆方法 联想/形像記法vcd4ncjxwpiogwpvtw7pj1lgx5mg/tctk9nd...

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

如下是google搜索出来的结果

详解介绍android:layout_gravity 和 android:gravity 之间的区别

记忆方法

联想/形像記法喎? 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

详解介绍android:layout_gravity 和 android:gravity 之间的区别

关键字 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);
}
  }
 }
喎?>