我无法让 float 操作按钮 (FAB) 出现在正确的位置。我希望它出现在标题和抽屉导航中的第一项之间。
目前,我已经让它出现在标题的右下角,而不是在第一个和第二个元素之间的线的顶部(第一个元素 = 标题和第二个元素 = recyclerview 中的第一个项目)。
我的应用使用了以下 appcompat 项目:
- appcompat-v7:23.0.0
- recyclerview-v7:23.0.0
- 设计:23.0.0
我正在使用抽屉导航,但我无法使用 NavigationView,因为我需要自定义项目条目而不是加载简单的菜单。
如您所知,抽屉实际上不是 2 个不同的控件。 header 实际上是 RecyclerView 中的“0”元素。我不知道这是否有所作为。
这是标题/“RecyclerView 中的 0 View ”的当前 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/navdrawer_image_height">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navDrawerHeaderView"
android:layout_width="match_parent"
android:layout_height="@dimen/navdrawer_image_height">
<ImageView
android:id="@+id/navdrawer_image"
android:layout_width="wrap_content"
android:layout_height="@dimen/navdrawer_image_height"
android:contentDescription="@string/cd_navdrawer_image"
android:scaleType="centerCrop"
android:src="@drawable/bg_material_design" />
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_image"
android:layout_width="@dimen/navdrawer_user_picture_size"
android:layout_height="@dimen/navdrawer_user_picture_size"
android:src="@drawable/ic_launcher"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:border_width="2dp"
app:border_color="#FF000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appNameTextView"
android:text="App Name"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/button_account"
app:layout_anchor="@id/navDrawerHeaderView"
app:layout_anchorGravity="bottom|right|end"
app:elevation="4dp"/>
</android.support.design.widget.CoordinatorLayout>
我想我可能将 FAB 放在了错误的位置/文件中。这是抽屉的 xml。
<?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"
android:id="@+id/drawerLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<!-- Content layout -->
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"/>
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/init_background">
</FrameLayout>
</LinearLayout>
<!-- Pages -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
帮助!!!!!
最佳答案
包含现有 RecyclerView
的示例抽屉 fragment 布局:
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:id="@+id/header"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:scrollbars="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:layout_margin="5dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
关于android - 无法使用 RecyclerView 在 DrawerLayout 中的标题和项目之间放置 float 操作按钮 (FAB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239948/