【问题标题】:ConstraintLayout with RecyclerView Top and Buttom views some part are invisible.带有 RecyclerView 顶部和底部视图的 ConstraintLayout 某些部分是不可见的。
【发布时间】:2018-10-14 17:48:47
【问题描述】:

从 2 个月开始,我一直在使用 ConstrainLayout,但每次我都会遇到一些问题,并且在花了很多时间之后我找到了解决方案,但现在我陷入了死锁。我的视图上部被状态栏隐藏,下部也被导航栏隐藏。

我的视图如何在设备中显示

在这里您可以看到我的 AppBar 位于状态栏下方,并且 RecyclerView 最后一项未显示完整。它的某些视图是不可见的。

我不确定问题出在哪里,以前我通过添加android:fitsSystemWindows="true" 来解决此类问题,但现在它无法正常工作。当我尝试应用谷歌结果时,它的行为也有所不同。

这是我的视图xml代码

<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="wrap_content"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_constraintBottom_toTopOf="@id/shop_recycler_view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/shop_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:fitsSystemWindows="true"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/app_bar"
    tools:listitem="@layout/shop_list_recycler_view_row" />
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-recyclerview overlap android-constraintlayout invisible


    【解决方案1】:

    我会使用CoordinatorLayout 而不是ConstraintLayout 来添加诸如隐藏Toolbar 等的未来功能。但是,使用CoordinatorLayout 似乎是这里的最佳选择:

    <android.support.design.widget.CoordinatorLayout 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"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/shop_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:fitsSystemWindows="true"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:listitem="@layout/shop_list_recycler_view_row" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 感谢您的回答,我以后会使用CoordinatorLayout,现在我通过将RecyclerView高度添加到0dp解决了这个问题。
    • 为什么是 0?您是否使用LinearLayout 作为RecyclerView 的根布局?您不需要将其设为 0。只需使用 match_parent。如果有帮助,请考虑接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多