如何将元素RecyclerView放在Android布局文件中的元素工具栏下?

时间:2017-09-14 09:41:46

标签: android android-layout layout android-recyclerview floating-action-button

嘿,我想在我的Android应用程序中,recyclerlistview和floatingActionButton没有剪切工具栏。这是我的布局:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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="match_parent">

            <include
                layout="@layout/toolbar_task_layout">
            </include>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView_task"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                app:layout_anchor="@id/recyclerView_task">
            </android.support.v7.widget.RecyclerView>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/floatingActionButton_task_openDialog_"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="16dp"
                android:src="@drawable/ic_add_black_36dp"
                app:layout_anchor="@id/recyclerView_task"
                android:clickable="true"
                app:fabSize="normal"
                app:layout_anchorGravity="bottom|right|end"/>


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

在这里你可以看到问题:

enter image description here

更新

这是工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
                                   android:id="@+id/toolbar_task"
                                   android:layout_alignParentTop="true"
                                   android:background="?attr/colorPrimary"
                                   android:elevation="6dp"
                                   android:minHeight="?attr/actionBarSize"
                                   app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                                   app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0 items selected"
            android:id="@+id/counter_task"
            android:textSize="18dp"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

3 个答案:

答案 0 :(得分:0)

尝试替换<include layout="@layout/toolbar_task_layout"></include>

用这个:

<include layout="@layout/toolbar_task_layout" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

编辑:我假设您还希望您的FAB位于右下方,因为这是常态,请尝试在FAB中设置android:layout_alignParentBottom="true"android:layout_alignParentRight="true"并使RecyclerViews高度匹配_parent

答案 1 :(得分:0)

尝试将toolbar android:layout_height 设为android:layout_height="?attr/actionBarSize"

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

答案 2 :(得分:0)

使用以下代码:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
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="match_parent">

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

    <include layout="@layout/toolbar_task_layout"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_task"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        app:layout_anchor="@id/recyclerView_task"
        android:layout_below="@+id/toolbar_task"/>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton_task_openDialog_"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    android:src="@mipmap/ic_launcher"
    app:layout_anchor="@id/recyclerView_task"
    android:clickable="true"
    app:fabSize="normal"
    app:layout_anchorGravity="bottom|right|end"/></android.support.design.widget.CoordinatorLayout>

我希望这会有所帮助......