What is the difference between gravity and layout_gravity in Android?

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

    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.

    And an example is here.

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

    Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.

    Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:

    1) To control the gravity of ALL child views of a LinearLayout container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.

    2) To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:

    import android.widget.LinearLayout.LayoutParams;
    import android.view.Gravity;
    ...
    
    Button button = (Button) findViewById(R.id.MyButtonId);
    // need to cast to LinearLayout.LayoutParams to access the gravity field
    LayoutParams params = (LayoutParams)button.getLayoutParams(); 
    params.gravity = Gravity.BOTTOM;
    button.setLayoutParams(params);
    

    For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravity value is -1 but Android put it center vertically.

    To change the horizontal positions of child views in a horizontal linear container, one can use layout_weight, margin and padding of the child view.

    Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or center_horizontal) and can be changed to left or right.

    Actually, a child view such as a button also has android:gravity XML attribute and the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.

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

    Look at the image to be clear about gravity

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

    If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".

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

    The android:gravity sets the gravity (position) of the children whereas the android:layout_gravity sets the position of the view itself. Hope it helps

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

    The difference

    android:layout_gravity is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.

    android:gravity is the Inside gravity of that View. Specifies in which direction its contents should align.

    HTML/CSS Equivalents

    Android                 | CSS
    ————————————————————————+————————————
    android:layout_gravity  | float
    android:gravity         | text-align
    

    Easy trick to help you remember

    Take layout-gravity as "Lay-outside-gravity".

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