Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_anchor and layout_anchorGravity do not work as expected

Does anyone know if this screenshot

enter image description here

reflects this layout correctly?

    <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#FFFFFF80"
        style="@android:style/TextAppearance.DeviceDefault.Large"
        android:text="@string/text_1"/>
    <TextView
        android:id="@+id/text_view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_anchor="@id/text_view1"
        app:layout_anchorGravity="bottom|right"
        android:background="#FFFF8080"
        style="@android:style/TextAppearance.DeviceDefault.Large"
        android:text="@string/text_2"/>
    <TextView
        android:id="@+id/text_view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_anchor="@id/text_view2"
        app:layout_anchorGravity="bottom|right"
        android:background="#FF8080FF"
        style="@android:style/TextAppearance.DeviceDefault.Large"
        android:text="@string/text_3"/>
</android.support.design.widget.CoordinatorLayout>

I was not expecting any overlap of text views, but may be my expectations are not correct?

like image 428
Y2i Avatar asked Nov 05 '15 13:11

Y2i


People also ask

What is Layout_anchor?

app:layout_anchor: This attribute can be set on children of the CoordinatorLayout to attach them to another view. The value would be the id of an anchor view that this view should position relative to. Note that, the anchor view can be any child View (a child of a child of a child of a CoordinatorLayout, for example).

What is CoordinatorLayout in android?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.


1 Answers

As @ana 01 mention in comment, you should add android:layout_gravity="right" in addition to app:layout_anchorGravity="right". By default app:layout_anchorGravityworks like if a dependent view aligns to an anchor's view by the center of the anchor's view.

So if you would like to align one view relatively to other, you should set an edge or gravity of a dependent view which will be used for alignment through out android:layout_gravity attribute.

like image 128
Sergei Vasilenko Avatar answered Sep 18 '22 08:09

Sergei Vasilenko