What is the difference between gravity and layout_gravity in Android?

前端 未结 20 1160
庸人自扰
庸人自扰 2020-11-21 11:41

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

    <
相关标签:
20条回答
  • 2020-11-21 12:33

    Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.

    0 讨论(0)
  • 2020-11-21 12:33

    Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.

    If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.

    I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.

    0 讨论(0)
提交回复
热议问题