Solution 1 :
You cant have scrolling effects in Toolbar. So AppBarLayout is used, see below images, you can achieve by using only AppBarLayout.
Solution 2 :
Toolbar
is just an usual View
and it may be placed anywhere you want, even few in one XML. AppBarLayout
is used to wrap Toolbar
(or other View
, e.g. TabLayout
) and add some scroll-behavior change possibilities, when placed above some scrolling container like ScrollView
or RecyclerView
(you probably want to use Nested
versions then)
check out nested classes listed in summary in on top of AppBarLayout
doc – all related to scrolling (e.g. OnOffsetChangedListener
or ScrollingViewBehavior
)
Problem :
The Code A is from the project architecture samples, the toolbar is wrapped with AppBarLayout.
I remove AppBarLayout and get Code B, and Code B works well just like Code A.
Must Toolbar be wrapped with AppBarLayout in Android Studio?
Code A (tasks_act.xml)
<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_id="@+id/drawer_layout"
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".tasks.TasksActivity"
tools_openDrawer="start">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical">
<com.google.android.material.appbar.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="wrap_content"
android_minHeight="?attr/actionBarSize"
android_theme="@style/Toolbar"
app_popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android_id="@+id/nav_host_fragment"
android_name="androidx.navigation.fragment.NavHostFragment"
android_layout_width="match_parent"
android_layout_height="match_parent"
app_defaultNavHost="true"
app_navGraph="@navigation/nav_graph" />
</LinearLayout>
<!-- Navigation Drawer -->
<com.google.android.material.navigation.NavigationView
android_id="@+id/nav_view"
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_layout_gravity="start"
android_fitsSystemWindows="true"
app_headerLayout="@layout/nav_header"
app_itemIconTint="@drawable/drawer_item_color"
app_itemTextColor="@drawable/drawer_item_color"
app_menu="@menu/drawer_actions" />
</androidx.drawerlayout.widget.DrawerLayout>
Code B (tasks_act.xml)
<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_id="@+id/drawer_layout"
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".tasks.TasksActivity"
tools_openDrawer="start">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical">
<androidx.appcompat.widget.Toolbar
android_id="@+id/toolbar"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_minHeight="?attr/actionBarSize"
android_theme="@style/Toolbar"
app_popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<fragment
android_id="@+id/nav_host_fragment"
android_name="androidx.navigation.fragment.NavHostFragment"
android_layout_width="match_parent"
android_layout_height="match_parent"
app_defaultNavHost="true"
app_navGraph="@navigation/nav_graph" />
</LinearLayout>
<!-- Navigation Drawer -->
<com.google.android.material.navigation.NavigationView
android_id="@+id/nav_view"
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_layout_gravity="start"
android_fitsSystemWindows="true"
app_headerLayout="@layout/nav_header"
app_itemIconTint="@drawable/drawer_item_color"
app_itemTextColor="@drawable/drawer_item_color"
app_menu="@menu/drawer_actions" />
</androidx.drawerlayout.widget.DrawerLayout>