只需在xml中设置即可,不需要Java代码,主要实现需要用到得属性
app:layout_scrollFlags="scroll|enterAlways"
app:contentInsetStart="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
具体代码如下;
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"