Android - RecyclerView 被 BottomNavigationView 遮挡

标签 android

我创建了默认的底部导航项目。在其中一个 fragment 中,我添加了一个 ListView,效果很好。然后我将其更改为 RecyclerView,它被 BottomNavigationView 遮挡了。我做错了什么?

这是主要 Activity 的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

这是 fragment 的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DiaryFragment">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        tools:listitem="@layout/recyclerview_item" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@android:drawable/ic_menu_add"
        android:layout_marginBottom="70dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        />

</android.support.constraint.ConstraintLayout>

RecyclerView 包含的行多于屏幕上显示的行数时,最后一行会部分出现在 BottomNavigationView 后面。如果有任何建议,我将不胜感激。

编辑 - 这是截图:

screenshot

编辑 #2:在第一个布局中,如果我将 BottomNavigationView 移动到 LinearLayout 中(感谢@Juanje),并更改其 android:layout_width0dpmatch_parent,然后 RecyclerView 不再被 BottomNavigationView 遮挡,但现在FloatingActionButton 随波逐流:

enter image description here

我正在修补它,如果有任何想法,我将不胜感激。

编辑#3

下面是我按照接受的答案加上其他一些调整后得到的结果。必要的更改是:

1) 在第一个布局中:

  • BottomNavigationView 移动到 LinearLayout
  • BottomNavigationView 中,将 android:layout_width"0dp" 更改为 "match_parent"

2) 在第二种布局中:

  • FloatingActionButton 中,更改 android:layout_marginBottom"70dp""10dp"

第一个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

第二个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DiaryFragment">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            tools:listitem="@layout/recyclerview_item" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@android:drawable/ic_menu_add"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        />

</android.support.constraint.ConstraintLayout>

screenshot

最佳答案

你需要设置一些约束,否则你的元素会重叠。使用这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/navigation"> <!--Added here and one line above; changed height: 0dp height means match constraint-->

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

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

关于Android - RecyclerView 被 BottomNavigationView 遮挡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52997332/

相关文章:

java - 使用Seek Bar缩放android Canvas

android - 在 Android 项目中找不到 ND4J 共享库

java - 使用 proguard 的 Android 混淆应用程序保留 jar 库

javascript - Android - WebView html5 localstorage 在应用程序更新后不持久

java - 在Java中将一个巨大的txt制作成数组的过程

android - 如何在 android 中将日期从 24 小时制更改为 12 小时制(上午/下午)

android - 让 ImageView 保持文本的正确大小,同时防止长文本将其推出 View ?

php - 如何在Android PHP MySQL中实现Session?

Android 文本转语音 语音转印度口音

android - 改造异步调用和屏幕旋转