简体   繁体   English

始终将浮动动作按钮置于顶部

[英]Put the Floating action button on Top always

I have Mainactivity, one Fragment and BlogRecycleadapter to populate the Fragment with Blog post. 我有Mainactivity,一个Fragment和BlogRecycleadapter,用于在Blog帖子中填充Fragment。

Inside Mainactivity, I have Floating action Button. 在Mainactivity内部,我有一个浮动动作按钮。 But When I run the app It hides beneath the items of blogRecycleadapter. 但是,当我运行该应用程序时,它隐藏在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 我的blogRecycleadapter类是

 <?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. 给出大于8dp的海拔高度并尝试。

<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: 尝试以下方法进行mainactivity布局:

 <?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): 尝试这样(颠倒FramLayout和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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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