Responsive Ads Here

ads

2019년 8월 18일 일요일

What is the difference between gravity and layout gravity in Android??

Quastion

I know we can set the following values to the android:gravity and  android:layout_gravity properties:


center
center_vertical
center_horizontal, etc.
But I am confused regarding both of these.

What is the difference between the usage of android:gravity and android:layout_gravity?




answer1

Their names should help you:

android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on.
android:layout_gravity sets the gravity of the View or Layout relative to its parent.



answer2

Inside - Outside

gravity arranges the content inside the view.
layout_gravity arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.



answer3

Inside - Outside

gravity arranges the content inside the view.
layout_gravity arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.

enter image description here

Notes
The layout_gravity does not work for views in a RelativeLayout. Use it for views in a LinearLayout or FrameLayout. See my supplemental answer for more details.
The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity are meaningless together.
The view's width (or height) has to be less than the parent. Otherwise layout_gravity won't have any effect. Thus, match_parent and layout_gravity are meaningless together.
The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=center only centers horizontally.
This answer only dealt with setting gravity and layout_gravity on the views within a layout. To see what happens when you set the gravity of the of the parent layout itself, check out the supplemental answer that I referred to above.
(Summary: gravity doesn't work well on a RelativeLayout but can be useful with a LinearLayout.)
So remember, layout_gravity arranges a view in its layout.
Gravity arranges the content inside the view.


댓글 없음:

댓글 쓰기

Does Python have a string 'contains' substring method?

Quastion I'm looking for a  string.contains  or  string.indexof  method in Python. I want to do: if not somestring . contain...

AD