【问题标题】:Floating action button hidden behind navigation drawer隐藏在导航抽屉后面的浮动操作按钮
【发布时间】:2016-05-17 07:01:38
【问题描述】:

我正在尝试向我的片段添加一个 fab,但它隐藏在导航栏后面。问题是出现布局对他的活动感兴趣,并且滚动行为设置为我附加片段的框架布局。如何将此行为添加到片段中的回收器视图中,而无需将工具栏添加到片段中。这是因为我有很多片段,只有一个活动 

Navigation_drawer_layout

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="close">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />
        </android.support.design.widget.AppBarLayout>


        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/NavigationDrawerStyle"
        app:itemIconTint="@color/PrimaryPurple"
        app:menu="@menu/activity_dasboard_drawer" />


</android.support.v4.widget.DrawerLayout>

片段布局

<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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/food_queue_recycle_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/title_white" />

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view_no_items"
            android:layout_width="350dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:visibility="gone"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="4dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

                <ImageView
                    android:id="@+id/errorImageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/empty_favourite_list"
                    android:src="@drawable/food_icon_empty" />

                <TextView
                    android:id="@+id/errorTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/errorImageView"
                    android:layout_marginTop="@dimen/large_text_size"
                    android:gravity="center"
                    android:text="@string/empty_favourite_list"
                    android:textAllCaps="true"
                    android:textColor="@color/SecondaryBlack"
                    android:textSize="@dimen/medium_text_size"
                    android:textStyle="bold" />
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/action_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_mode_edit_48px"
        app:backgroundTint="@color/PrimaryPurple"
        app:layout_anchor="@id/food_queue_recycle_list"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="helpers.FABCoordinatorBehavior" />
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 这种类型的问题现在在将 FloatingActionButton 放入 Fragment 时很常见。 FAB 的真正意义在于进入 Activity,以便它们可以动画进出。将您的 CoordinatorLayout 和 FAB 移动到 Activity 布局,并在 Fragment 中添加 RecyclerView。在Activity中,在需要的时候调用FAB上的show()和hide()来显示。
  • 好的,谢谢您的确认。
  • 此处有类似问题,但与您的设置略有不同:stackoverflow.com/questions/35194672/…
  • @Rohit_Ramkumar - 那些CardViews 是什么?我的意思是,是 RecyclerView 项目吗?
  • @LinX64 也让我失望了,但看起来这只是一个默认的 CardView,如果列表中有条目,他可能会设置为 View.GONE。这里显示的 CardView 实际上在 RecyclerView 之外。

标签: android android-coordinatorlayout floating-action-button


【解决方案1】:

Daniel Nugent 说得对。你可以这样做:

只需将FloatingActionButton 放在主布局上,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="close">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

        </android.support.design.widget.AppBarLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/action_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_mode_edit_48px"
            app:backgroundTint="@color/PrimaryPurple"
            app:layout_anchor="@id/food_queue_recycle_list"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_behavior="helpers.FABCoordinatorBehavior" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/NavigationDrawerStyle"
        app:itemIconTint="@color/PrimaryPurple"
        app:menu="@menu/activity_dasboard_drawer" />

</android.support.v4.widget.DrawerLayout>

然后在 Fragment 中使用 RecyclerView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/food_queue_recycle_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/title_white" />

    <!-- not sure about this, but, it shouldn't be here -->

    <!--<android.support.v7.widget.CardView
        android:id="@+id/card_view_no_items"
        android:layout_width="350dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:visibility="gone"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageView
                android:id="@+id/errorImageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="@string/empty_favourite_list"
                android:src="@drawable/food_icon_empty" />

            <TextView
                android:id="@+id/errorTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/errorImageView"
                android:layout_marginTop="@dimen/large_text_size"
                android:gravity="center"
                android:text="@string/empty_favourite_list"
                android:textAllCaps="true"
                android:textColor="@color/SecondaryBlack"
                android:textSize="@dimen/medium_text_size"
                android:textStyle="bold" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>-->

</RelativeLayout>

我不确定CardView,因为您将RecyclerView 设置为match_parent

<android.support.v7.widget.RecyclerView
        android:id="@+id/food_queue_recycle_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/title_white" />

所以,那些卡不应该在那里。(unless you need to show it as error cards)通过将setVisibility设置为GONE,然后处理是否有错误,显示为:setVisibility == View.VISIBLE .

【讨论】:

  • 那些cardviews是没有item时的错误cardview。它们与回收站物品不同。
  • 已添加到答案中。
猜你喜欢
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多