【问题标题】:Recycler View is Overriding the ToolbarMenuRecycler View 正在覆盖 ToolbarMenu
【发布时间】:2020-05-22 06:36:27
【问题描述】:

这是我的 Xml 代码:-

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

<androidx.drawerlayout.widget.DrawerLayout
        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=".MainActivity"
        android:id="@+id/drawerLayout"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar1"
                layout="@layout/toolbar_standalone" />
        </LinearLayout>

        <!--    Navigation View Describe here input are header and toolbar-->
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:itemIconTint="#8C000000"
            app:itemTextColor="#E6000000"
            app:menu="@menu/navigation_menu_items_grouped">

        </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

    <!--recycler view add here-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">
    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

这里 Recycler View 出现在工具栏上方。 工具栏未显示在模拟器屏幕中。 回收站视图工作正常,但工具栏及其菜单未显示。 请让我更正布局以解决此问题。 请让我知道在 XML 文件中需要做哪些更改。

【问题讨论】:

  • 将&lt;RecyclerView&gt; 移动到&lt;LinearLayout&gt; 中,在&lt;include&gt; 之后,并完全去掉外部的ConstraintLayout。

标签: android android-recyclerview navigation-drawer toolbar


【解决方案1】:

你的 RecyclerView 有

    app:layout_constraintTop_toTopOf="parent"

它从父级创建基线。所以改变这个。

<!--recycler view add here-->
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/drawerLayout"   //change this
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">
</androidx.recyclerview.widget.RecyclerView>

【讨论】:

  • 不工作,Recycler View 项目未显示。
【解决方案2】:

S T 答案是对的,只是倒过来了。试试这个:

而不是bottomToTop 应该是'topToBottom`,因为你想把recyclerview 顶部靠近抽屉底部。

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/drawerLayout"   //change this
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">
</androidx.recyclerview.widget.RecyclerView>

【讨论】:

    【解决方案3】:

    这对我有用:-

     <LinearLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <include
                    android:id="@+id/toolbar1"
                    layout="@layout/toolbar_standalone" />
    
                <!--recycler view add here-->
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
    
            </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多