我试图让FAB超过recyclerview,在我的情况下将覆盖整个屏幕。 FAB不会显示甚至recyclerview是空的。以下是我的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上尝试了这个没有navigationdrawer(简单的活动,它在那里工作,即在listview上显示)。
任何帮助将不胜感激。谢谢
[编辑:]我认为问题是由于父布局,即android.support.v4.widget.DrawerLayout,我复制粘贴代码从开始到结束到另一个空活动,它显示浮动按钮在那里。但仍然无法弄清楚如何解决这个问题,我需要在Drawerlayout中显示浮动操作按钮。
试试这样:
删除所有布局。
只保留一个父母,那就是CoordinatorLayout
。
在CoordinatorLayout
内,把你的RecyclerView
和FloatingActionButton
。
CoordinatorLayout
应该自动安排您的工具栏,回收器和工厂,因为它被编程为处理设计支持库组件。
这是一个例子。您可以使用RecyclerView
代替FrameLayout(在运行时动态加载片段)。我通常在另一个RecyclerView
中使用Fragment
并在运行时加载片段。这样可以保持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>
尝试使用FrameLayout而不是LinearLayout作为根布局。这对我有用。
你可以使用LinearLayout,这似乎不是问题。
我非常确定您的解决方案将通过在FAB按钮下移动RecyclerView来解决。
换句话说,在XML布局中,通过RecyclerView代码块移动FAB按钮代码块。
一个例子:
<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按钮现在至少应该可见。
我刚刚创建了一个新的活动,并在那里复制粘贴了整个代码,并在那里工作正常。我还是不知道出了什么问题。但它以这种方式对我有用。我在两个活动中比较了逐行代码,它们是相同的。它只是浮动动作按钮显示在新活动而不是旧活动中。
感谢所有尝试的社区的帮助。
应该是因为androidmanifest.xml。添加新活动时,会自动将其添加到androidmanifest.xml中并获得权限。