【问题标题】:Prevent AppBarLayout.ScrollingViewBehavior from adding top margin to the view?防止 AppBarLayout.ScrollingViewBehavior 向视图添加上边距?
【发布时间】:2019-04-09 08:22:37
【问题描述】:

我正在尝试实现一个布局,其中我的视图将在屏幕上一直延伸到ActionBar 下方,并且 ActionBar 将是透明的。我还需要ActionBarBottomNavigationView 来隐藏滚动,所以我使用CoordinatorLayout

我正在使用属性为<item name="windowActionBarOverlay">true</item> 的AppCompat 主题,以便在ActionBar 下扩展我的视图。但是,在将app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到我的容器片段后,视图将放置在 ActionBar 下方。

有没有办法防止这部分appbar_scrolling_behaviour?我已经尝试过没有这种行为,但是 AppBar 的滚动与 RecyclerView 在其下方滚动不同步。谢谢!

这是gif of the problem

P.S.:我正在尝试使用 Android 导航组件,所以我正在关注单个活动、多片段架构,如果这很重要的话。

【问题讨论】:

    标签: android android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    尝试这段代码并进行一些更改,因为我使用的是 androidx.. 像这样制作主布局..

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@color/white">
    
    
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:theme="@style/MyToolbar"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:titleTextAppearance="@style/MyToolbar">
    
            <ImageView
                android:id="@+id/headerIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/_6sdp"
                android:src="@drawable/ic_edit_location_black"
                android:visibility="gone" />
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/_6sdp">
    
                <TextView
                    android:id="@+id/header"
                    style="@style/MyToolbar"
                    android:layout_width="wrap_content"
                    android:layout_height="?attr/actionBarSize"
                    android:gravity="center_vertical"
                    android:textColor="@color/primary"
                    app:layout_constraintStart_toStartOf="parent" />
    
                <ImageView
                    android:id="@+id/ivSearch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/_10sdp"
                    android:src="@drawable/search_icon"
                    android:visibility="gone"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.appcompat.widget.Toolbar>
    
    
    </com.google.android.material.appbar.AppBarLayout>
    
    <FrameLayout
        android:id="@+id/activity_main_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomContainer"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />
    
    
        <RelativeLayout
           android:id="@+id/bottomContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
    
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bvTabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/Widget.BottomNavigationView"
                app:itemIconTint="@drawable/navigation_drawable_tint"
                app:itemTextColor="@drawable/navigation_drawable_tint"
                app:menu="@menu/bottom_menu_item" />
    
    
        </RelativeLayout>
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 感谢您的回复,但是,使用您的布局并不会在滚动时隐藏 ActionBar 或底部导航栏。这就是我使用CoordinatorLayout 的原因。
    • 因为根据我的代码 appbar 和底部定义内容部分之间。当您加载该内容部分的片段时,您只滚动该部分。那个时候你的 appbar 和 bottombar 不动
    • 您找到解决问题的方法了吗?
    猜你喜欢
    • 2015-10-09
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多