android中gravity什么意思,详解介绍android:layout_gravity 和 android:gravity 之间的区别
android開發必遇問題,最有可能忘記兩者之間的區別的問題之一
如下是google搜索出來的結果
記憶方法
聯想/形像記法喎? 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
關鍵字 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);
}
}
}
喎?>
總結
以上是生活随笔為你收集整理的android中gravity什么意思,详解介绍android:layout_gravity 和 android:gravity 之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python入门认知学习程序
- 下一篇: 联芯Lc1860和LC1860C智能手机