简体   繁体   中英

Put the Floating action button on Top always

I have Mainactivity, one Fragment and BlogRecycleadapter to populate the Fragment with Blog post.

Inside Mainactivity, I have Floating action Button. But When I run the app It hides beneath the items of blogRecycleadapter.

How can I make Floating action button always on Top??

在此处输入图片说明

My mainactivity layout is

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".Main2Activity"> <android.support.v7.widget.Toolbar android:id="@+id/main_toolbar" app:titleTextAppearance="@style/Toolbar.TitleText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Light" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"></android.support.v7.widget.Toolbar> <android.support.design.widget.BottomNavigationView android:id="@+id/mainBottomNav" android:background="@color/colorblog" android:layout_width="0dp" android:layout_height="wrap_content" app:menu="@menu/bottom_menu" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <android.support.design.widget.FloatingActionButton android:id="@+id/add_post_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:clickable="true" android:layout_gravity="bottom|end" android:focusable="true" android:visibility="visible" app:elevation="6dp" app:layout_constraintBottom_toTopOf="@+id/mainBottomNav" app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@mipmap/action_add" /> <FrameLayout android:id="@+id/main_container" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/mainBottomNav" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/main_toolbar" app:layout_constraintVertical_bias="1.0"> </FrameLayout> </android.support.constraint.ConstraintLayout> 

and my fragment layout is ,

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/blog_list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> 

and my blogRecycleadapter class is

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="wrap_content"> <android.support.v7.widget.CardView android:id="@+id/main_blog_post" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:padding="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/blog_like_btn" android:layout_width="32dp" android:layout_height="32dp" android:layout_marginBottom="8dp" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/blog_desc" app:srcCompat="@mipmap/action_like_gray" /> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/blog_user_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:src="@drawable/profile_placeholder" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/blog_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:text="Username" android:textStyle="bold" app:layout_constraintStart_toEndOf="@+id/blog_user_image" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/blog_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:text="Blog Post Date" app:layout_constraintStart_toEndOf="@+id/blog_user_image" app:layout_constraintTop_toBottomOf="@+id/blog_user_name" /> <ImageView android:id="@+id/blog_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:scaleType="centerCrop" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/blog_user_image" app:srcCompat="@drawable/image_placeholder" /> <TextView android:id="@+id/blog_desc" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:text="Lorem ipsum dolor sit amet, soluta regione urbanitas vis in, qui elit populo ut. " app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/blog_image" /> <TextView android:id="@+id/blog_like_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="0 Likes" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/blog_like_btn" app:layout_constraintTop_toBottomOf="@+id/blog_desc" /> </android.support.constraint.ConstraintLayout> </android.support.v7.widget.CardView> </android.support.constraint.ConstraintLayout> 

You can increase elevation until it gets visible over bottom navigation. Give elevation greater than 8dp and try.

<RelativeLayout 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">

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Light" />


<android.support.design.widget.BottomNavigationView
    android:id="@+id/mainBottomNav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottom_bar_menu" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_post_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/mainBottomNav"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    android:clickable="true"
    android:focusable="true"
    android:visibility="visible"
    app:elevation="10dp" />


<FrameLayout
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/mainBottomNav"
    android:layout_below="@+id/main_toolbar">

</FrameLayout>

</RelativeLayout>

just load your fragment in main container.. hope this works

Try This for you mainactivity layout:

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <android.support.v7.widget.Toolbar android:id="@+id/main_toolbar" app:titleTextAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Light" /> <FrameLayout android:id="@+id/main_container" android:layout_width="match_parent" android:layout_height="wrap_content"> </FrameLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/mainBottomNav" android:background="@color/list_background" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/bottom_menu" android:layout_alignParentBottom="true"/> </RelativeLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/add_post_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="?attr/actionBarSize" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:clickable="true" android:layout_gravity="bottom|end" android:focusable="true" android:visibility="visible" app:elevation="6dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@mipmap/ic_launcher" /> </android.support.constraint.ConstraintLayout> 

Hope this helps.

Try like this (inversed the position of FramLayout and FloatingActionButton):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".Main2Activity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></android.support.v7.widget.Toolbar>



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/mainBottomNav"
        android:background="@color/colorblog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></FrameLayout>
    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/mainBottomNav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_toolbar"
        app:layout_constraintVertical_bias="1.0">

    </FrameLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_post_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:clickable="true"
        android:layout_gravity="bottom|end"
        android:focusable="true"
        android:visibility="visible"
        app:elevation="6dp"
        app:layout_constraintBottom_toTopOf="@+id/mainBottomNav"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@mipmap/action_add" />


</android.support.constraint.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM