What is the difference between gravity and layout_gravity in Android?

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

    gravity: is used for simple views like textview, edittext etc.

    layout_gravity: is used for current view only gravity in context of it's relative parent view like linear Layout or FrameLayout to make view in center or any other gravity of its parent.

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

    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.

    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.

    xml

    Here is the xml for the above image for your reference:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#e3e2ad"
            android:orientation="vertical" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textSize="24sp"
                android:text="gravity=" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:background="#bcf5b1"
                android:gravity="left"
                android:text="left" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:background="#aacaff"
                android:gravity="center_horizontal"
                android:text="center_horizontal" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:background="#bcf5b1"
                android:gravity="right"
                android:text="right" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:background="#aacaff"
                android:gravity="center"
                android:text="center" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#d6c6cd"
            android:orientation="vertical" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textSize="24sp"
                android:text="layout_gravity=" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:layout_gravity="left"
                android:background="#bcf5b1"
                android:text="left" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:layout_gravity="center_horizontal"
                android:background="#aacaff"
                android:text="center_horizontal" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:layout_gravity="right"
                android:background="#bcf5b1"
                android:text="right" />
    
            <TextView
                android:layout_width="200dp"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:background="#aacaff"
                android:text="center" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    Related

    • Difference between a View's Padding and Margin
    • Match_parent vs wrap_content
    • How to set both gravity and layout gravity of a LinearLayout programatically
    0 讨论(0)
  • 2020-11-21 12:16

    Gravity: Allow you move the content inside a container. (How sub-views will be placed).

    Important: (MOVE along X-axis or Y-axis within available space).

    Example: Let's say if you were to work with LinearLayout (Height: match_parent, Width: match_parent) as root level element, then you will have full frame space available; and the child views says 2 TextViews (Height: wrap_content, Width: wrap_content) inside the LinearLayout can be moved around along x/y axis using corresponding values for gravity on parent.

    Layout_Gravity: Allow you to override the parent gravity behavior ONLY along x-axis.

    Important: (MOVE[override] along X-axis within available space).

    Example: If you keep in mind the previous example, we know gravity enabled us to move along x/y axis, i.e; the place TextViews inside LinearLayout. Let's just say LinearLayout specifies gravity: center; meaning every TextView needs to be center both vertically and horizontally. Now if we want one of the TextView to go left/right, we can override the specified gravity behavior using layout_gravity on the TextView.

    Bonus: if you dig deeper, you will find out that text within the TextView act as sub-view; so if you apply the gravity on TextView, the text inside the TextView will move around. (the entire concept apply here too)

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

    There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).

    Use Of Gravity and Layout-gravity in FrameLayout .....

    Note:-

    1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.

    2. Layout-gravity is used with the parent View as some User have answer.

    3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....

    4. We can set Child View any where in the FrameLayout using layout-gravity .

    5. We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .

    6. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).

    have look on example ......

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:textSize="25dp"
            android:background="#000"
            android:textColor="#264bd1"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="Center Layout Gravity"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:textColor="#1b64b9"
            android:gravity="bottom"
            android:layout_gravity="bottom|center"
            android:text="Bottom Layout Gravity" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:textColor="#d75d1c"
            android:gravity="top"
            android:layout_gravity="top|center"
            android:text="Top Layout Gravity"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginTop="100dp"
            android:textColor="#d71f1c"
            android:gravity="top|right"
            android:layout_gravity="top|right"
            android:text="Top Right Layout Gravity"/>
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginBottom="100dp"
            android:textColor="#d71cb2"
            android:layout_gravity="bottom"
            android:gravity="bottom"
            android:text="Top Left Layout Gravity"/>
    
    </FrameLayout>
    

    Output:-

    Use Of Gravity and Layout-gravity in LinearLayout .....

    Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.

    LinearLayout with orientation vertical ....

    Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).

    have look on example :-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:textSize="25dp"
            android:background="#000"
            android:textColor="#264bd1"
            android:gravity="center"
            android:layout_gravity="center_horizontal"
            android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginTop="20dp"
            android:textColor="#d75d1c"
            android:layout_gravity="right"
            android:text="Right Layout Gravity"/>
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginBottom="100dp"
            android:textColor="#d71cb2"
            android:layout_gravity="left"
            android:layout_marginTop="20dp"
            android:gravity="bottom"
            android:text="Left Layout Gravity"/>
    
    </LinearLayout>
    

    Output:-

    LinearLayout with orientation horizontal ....

    Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).

    have look on example :-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:textSize="25dp"
            android:background="#000"
            android:textColor="#264bd1"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:text="Bottom \nLayout \nGravity"/>
    
        <TextView
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginTop="20dp"
            android:textColor="#d75d1c"
            android:layout_gravity="center"
            android:text="Center \nLayout \nGravity"/>
    
    
        <TextView
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:textSize="25dp"
            android:background="#000"
            android:layout_marginBottom="100dp"
            android:textColor="#d71cb2"
            android:layout_gravity="left"
            android:layout_marginTop="20dp"
            android:text="Left \nLayout \nGravity"/>
    
    </LinearLayout>
    

    output:-

    Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....

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

    gravity--Applies to its own view.

    layout-gravity---Applies to view related to its parent.

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

    An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.

    Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view

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