浮动操作按钮未显示在抽屉布局中的RecyclerView上。

17

我正在尝试在RecyclerView上方获取一个FAB,它将覆盖整个屏幕。即使RecyclerView为空,FAB也不会显示。以下是我的xml代码。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.technistan.ledger.CreateLedger"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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


        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:id="@+id/recyclerView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                />


            <android.support.design.widget.FloatingActionButton
                android:id="@+id/myFAB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="20dp"
                app:elevation="4dp" />


        </FrameLayout>

    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="com.technistan.ledger.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />


    </android.support.v4.widget.DrawerLayout>
预览中显示浮动按钮正常,如下图所示: https://www.dropbox.com/s/18u5pt5v6kkkibj/Screen%20Shot%202015-09-13%20at%201.19.20%20PM.png?dl=0 但是当我运行应用程序时,没有显示FAB。我尝试了许多组合,但都没有成功。 我在没有导航抽屉的listview上尝试过(简单的activity,在那里可以显示在listview上)。
希望有人能提供帮助。谢谢。
[编辑:] 我认为问题是由于父布局即android.support.v4.widget.DrawerLayout引起的,我复制粘贴了从开头到其结尾的代码到另一个空活动中,它在那里显示了浮动按钮。但仍无法解决此问题,我需要在Drawerlayout中显示浮动操作按钮。

你尝试过将大小更改为固定值吗? - dieter
需要精确到哪些尺寸? - Tariq Mahmud
为了确保FloatingActionButton确实不可见,请检查您的容器(LinearLayout,FrameLayout)中的布局。 - dieter
是的!但这就是问题所在,因为它在活动中显示,而没有像我上面提到的DrawerLayout。[编辑] - Tariq Mahmud
1
@Nininea 需要查看您的布局代码才能找到确切的问题。但是尝试使用CoordinatorLayout而不是FrameLayout。 - Tariq Mahmud
显示剩余2条评论
5个回答

17

试试这样做:

删除所有布局。

只保留一个父级元素,即CoordinatorLayout

CoordinatorLayout中,放置您的RecyclerViewFloatingActionButton

CoordinatorLayout应该自动排列您的工具栏、Recycler和Fab,因为它被编程处理设计支持库组件。

以下是一个例子。您可以使用RecyclerView代替FrameLayout(在运行时动态加载片段)。我通常在另一个片段中使用RecyclerView,并在此处运行时加载片段。这将使xml文件保持干净。

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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


    <!-- Main layout for fragment placing -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        app:fab_colorNormal="@color/pink"
        app:fab_colorPressed="@color/pink_pressed"
        app:borderWidth = "0dp" />


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

<!-- Nav drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/pink_pressed"
    app:itemTextColor="@color/primary_text"
    app:menu="@menu/nav_menu" />

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

你也可以跟随这个不错的教程来了解这些内容:http://inthecheesefactory.com/blog/android-design-support-library-codelab/en - anandbibek
为什么这个答案没有被选中。不管怎样,它帮了我,谢谢! - Yasin Kaçmaz

1

尝试使用FrameLayout而不是LinearLayout作为您的根布局。这对我有用。


-2

这应该是由于androidmanifest.xml文件引起的。 当您添加新活动时,它会自动添加到androidmanifest.xml文件中,并具有正确的权限。


-2

我刚刚创建了一个新的活动,并将整个代码复制粘贴到那里,它在那里运行得很好。我仍然不知道问题出在哪里。但是这种方式对我有效。我逐行比较了两个活动中的代码,它们是相同的。只是浮动操作按钮显示在新活动中而不是旧活动中。

无论如何,感谢社区提供的所有帮助。


也许问题出在Activity的代码而不是布局的代码。 - BladeCoder
但是它对活动也做了同样的事情,将逻辑代码复制粘贴到新的类文件中。我仍在寻找我在那里做错了什么。 - Tariq Mahmud

-2

你可以使用LinearLayout,这似乎不是问题。

我相信将RecyclerView移动到FAB按钮下方可以解决你的问题。

换句话说,在XML布局中,将FAB按钮代码块移到RecyclerView代码块上方。

以下是一个示例:

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/myFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        app:elevation="4dp" />

    <android.support.v7.widget.RecyclerView
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

由于Android系统绘制视图的方式,顺序很重要。

您可能会看到其他工件,但FAB按钮现在至少应该可见。


我按照您的要求操作了,但仍然无法看到FAB。这是我的代码和预览截图链接。在预览中它显示正常。https://drive.google.com/file/d/0B5nwHKjoiUubTnF0cnhsUFBRSUU/view?usp=sharing - Tariq Mahmud
这是在模拟器上的显示方式:https://drive.google.com/file/d/0B5nwHKjoiUubOWRuMnRHVU5PbE0/view?usp=sharing - Tariq Mahmud
@tariq 在你的截图中,似乎你已经在 RecyclerView 之前声明了你的浮动操作按钮。尝试交换顺序,让 RecyclerView 先出现。另外,你的模拟器是什么级别的?因为高程只在 Lollipop 及以上版本中反映。如果还不行,你能否在开发者选项中打开“显示布局边界”并截图呢? - Gary
在旧版本的Android上,XML中出现的最后一个视图总是绘制在顶部。这个解决方案是不正确的。 - BladeCoder
@Gary 我尝试了在RecyclerView下面和上面添加浮动操作按钮,但它们都没有显示出来。最后我创建了一个新的活动并将代码粘贴到那里,它就起作用了。无论如何,还是谢谢你的帮助 :) - Tariq Mahmud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接