Friday, July 1, 2016

android - CoordinatorLayout anchor and gravity example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true"
    android:background="#ebf5ee"
    >
    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center text"
        android:background="#ff0000"
        android:gravity="center"
        android:padding="30dp"
        android:layout_gravity="center"
        android:layout_margin="25dp"
        />
    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="left"
        android:background="#3163d7"
        android:padding="30dp"
        app:layout_anchor="@id/tv_center"
        app:layout_anchorGravity="left|start|center_vertical"
        android:layout_gravity="center|start"
        />
    <TextView
        android:id="@+id/tv_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="top"
        android:background="#72c493"
        android:padding="30dp"
        app:layout_anchor="@id/tv_center"
        app:layout_anchorGravity="top|center_horizontal"
        android:layout_gravity="center|top"
        />
    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:background="#ad56bc"
        android:padding="30dp"
        app:layout_anchor="@id/tv_center"
        app:layout_anchorGravity="right|center_vertical"
        android:layout_gravity="center|end"
        />
    <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bottom"
        android:background="#a37551"
        android:padding="30dp"
        app:layout_anchor="@id/tv_center"
        app:layout_anchorGravity="bottom|center_horizontal"
        android:layout_gravity="center|bottom"
        />
</android.support.design.widget.CoordinatorLayout>