RecyclerView在工具栏下方。

4

问题: 我有一个RecyclerView和一个Toolbar在一个CoordinatorLayout中。RecyclerViewToolbar下面,但它不应该。

enter image description here

问题: 我如何让RecyclerViewToolbar有一个边框?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

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

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

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

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

maschine_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:scrollbars="vertical" />

</LinearLayout>

将“android:layout_marginTop =”20dp“”属性应用于RecyclerView。 - Jatin Devani
2
app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到你的FrameLayout中。 - V-rund Puro-hit
3个回答

8

只需将以下行添加到父布局即maschine_fragment.xmlLinearlayout中:

xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"

否则将此行添加到activity_main.xml的Framelayout中。
app:layout_behavior="@string/appbar_scrolling_view_behavior"

它会适用于你!:)

3
将您的代码更改为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior = "@string/appbar_scrolling_view_behavior" />

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

2
如果您在主布局中使用FrameLayout作为容器,并且有一个单独的布局包含RecyclerView - 在这种情况下,您应该将'app:layout_behavior=...'添加到主布局中的FrameLayout! - Kirill Karmazin

0
尝试将以下行添加到`maschine_fragment.xml`文件中,放在线性布局的android:layout_height="match_parent"下方。
android:paddingTop="?attr/actionBarSize"

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