9

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?

4
  • 5
    Yes, the outcome is expected: the center of text_view3 is anchored to the right bottom corner of text_view2
    – DmitryArc
    Commented Nov 5, 2015 at 14:23
  • 1
    So the question really is: How do I move a View to right of a view in a cordinatorLayout...? I'm wondering the same
    – Wärting
    Commented Feb 1, 2016 at 8:10
  • @Wärting I ended up creating a custom behavior to lay child views out. May be there are easier ways of doing that which I haven't found out yet.
    – Y2i
    Commented Feb 1, 2016 at 13:54
  • 10
    @Wärting I think you only need to add android:layout_gravity="right" in addition to app:layout_anchorGravity="right". Commented Feb 7, 2017 at 22:40

1 Answer 1

35

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.