What is the difference between gravity and layout_gravity in Android?

前端 未结 20 1161
庸人自扰
庸人自扰 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:22

    android:gravity -> Sets the gravity of the content of the View its used on.

    android:layout_gravity -> Sets the gravity of it's Parent's view or Layout

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

    android:gravity is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view.

    android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.

    For more details you can visit

    http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

    0 讨论(0)
  • 2020-11-21 12:24
    android:gravity
    

    is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"

    android:layout_gravity 
    

    is used for view itself relative to the parent or layout file.

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

    The basic difference between the two is that-

    android:gravity is used for child elements of the view.

    android:layout_gravity is used for this element with respect to parent view.

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

    From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.

    I think this is right but the best way to find out is to play around.

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

    Gravity is used to set text alignment in views but layout_gravity is use to set views it self. Lets take an example if you want to align text written in editText then use gravity and you want align this editText or any button or any view then use layout_gravity, So its very simple.

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