Resolution of conflict between androids: gravity and android: layout_gravity

Consider the following trivial layout.

<?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:gravity="bottom"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="top" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

external LinearLayout indicates that it has

android:gravity="bottom"

which means that he wants his children to "fall to the bottom."

internal LinearLayout indicates that

android:layout_gravity="top"

which means that he wants him to be laid out at the top of his closing layout. (To indicate android:layout_gravity, you need to apparently edit the XML file directly. There seems to be no way to get this attribute from Eclipse. Anyway ...)

How does Android resolve this conflict? The example above assumes the attribute gravityoverrides the attribute layout_gravity. So how is the conflict resolved at all?

+4
1

layoutVertical() LinearLayout, gravity LinearLayout .

layout_gravity " " ( -).

-:

    final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;

    switch (majorGravity) {
       case Gravity.BOTTOM:
           childTop = ... 
           break;

       // ... same for CENTER_VERTICAL and TOP
    }

:

    for (int i = 0; i < count; i++) {
        final View child = getVirtualChildAt(i);

        final LinearLayout.LayoutParams lp =
                (LinearLayout.LayoutParams) child.getLayoutParams();

        int gravity = lp.gravity;
        if (gravity < 0) {
            gravity = minorGravity;
        }

        final int layoutDirection = getLayoutDirection();
        final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
        switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                childLeft = ...
                break;

            // ... same for RIGHT and LEFT

layoutHorizontal().

, Java , android:layout_gravity. gravity LinearLayout.LayoutParams ( LayoutParams ).

+1

All Articles