why fab set under cardview with layout_anchorGravity

4033 views android
9

<?xml version="1.0" encoding="utf-8"?>
            <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/activity_login"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary">

                <android.support.v7.widget.CardView
                    android:id="@+id/card_authentication_container"
                    
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_above="@id/button_authentication"
                    android:layout_gravity="center_horizontal|top"
                    android:layout_marginTop="32dp"
                    app:cardCornerRadius="10dp"
                    app:cardElevation="10dp">
                </android.support.v7.widget.CardView>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/float_button_authenticate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/gamepad"
                    app:fabSize="mini"
                    app:layout_anchor="@id/card_authentication_container"
                    app:layout_anchorGravity="bottom|center_horizontal" />
            </android.support.design.widget.CoordinatorLayout>

when i try to set floating action button on cardview with app:layout_anchorGravity="bottom|center_horizontal" ,it set under cardview. like this

answered question

1 Answer

5

The problem is with your:

app:cardElevation="10dp"

Since you CardView elevation will be greater than the one in your FloatingActionButton, it will always appear behind.

Explanation: from the documentation:

Elevation is the relative distance between two surfaces along the z-axis.

So in terms of the z-axis, your CardView is in a much higher place (10dp) than your FAB (since you didn't define any elevation value it will use the default which is 0dp).


Solution: Set an elevation in your FAB with the value 10dp or greater.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories