一、普通抽屉,抽屉在标题栏下方

  • 1.效果图

iShot2020-08-2623.22.29.gif

  • 2.布局文件: ``` <?xml version=”1.0” encoding=”utf-8”?>

  1. <TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:text="@string/app_name"
  5. android:textSize="17dp"
  6. android:textColor="@color/white"
  7. android:gravity="center"
  8. android:background="@color/red_FF8EB7"/>
  1. <com.google.android.material.navigation.NavigationView
  2. android:id="@+id/nv_drawer_bottom_navigation"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:layout_gravity="start"
  6. app:headerLayout="@layout/drawer_header"
  7. android:background="@color/white"
  8. app:menu="@menu/drawer_menu"/>
  9. </com.kiwilss.xview.design.drawer.MyDrawerLayout>

  1. - 3. activity:

class DrawerBottomActivity: AppCompatActivity(R.layout.activity_drawer_bottom) { @SuppressLint(“ResourceType”) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

  1. tb_drawer_bottom_toolbar.title = "简单抽屉"
  2. tb_drawer_bottom_toolbar.setTitleTextColor(ContextCompat.getColor(this,R.color.blue_74D3FF))
  3. setSupportActionBar(tb_drawer_bottom_toolbar)
  4. //创建返回键,并实现打开关/闭监听
  5. val actionBarDrawerToggle = ActionBarDrawerToggle(
  6. this,dl_drawer_bottom_drawer,tb_drawer_bottom_toolbar,
  7. R.string.navigation_drawer_open, R.string.navigation_drawer_close)
  8. actionBarDrawerToggle.syncState()
  9. dl_drawer_bottom_drawer.addDrawerListener(actionBarDrawerToggle)
  10. //抽屉打开关闭监听
  11. dl_drawer_bottom_drawer.addDrawerListener(object :DrawerLayout.DrawerListener{
  12. override fun onDrawerStateChanged(newState: Int) {
  13. }
  14. override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
  15. }
  16. override fun onDrawerClosed(drawerView: View) {
  17. }
  18. override fun onDrawerOpened(drawerView: View) {
  19. }
  20. })
  21. //侧滑抽屉点击监听
  22. nv_drawer_bottom_navigation.run {
  23. //设置每个文字和图片颜色变化
  24. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  25. itemTextColor = resources.getColorStateList(R.drawable.nav_menu_text_color, null)
  26. itemIconTintList = resources.getColorStateList(R.drawable.nav_menu_text_color, null)
  27. }
  28. //设置默认选中
  29. setCheckedItem(R.id.nav_home)
  30. setNavigationItemSelectedListener {
  31. Snackbar.make(nv_drawer_bottom_navigation, "hello", Snackbar.LENGTH_LONG)
  32. .setAction("Action", null).show()
  33. dl_drawer_bottom_drawer.close()
  34. true
  35. }
  36. }
  37. }

}

  1. <a name="ricuQ"></a>
  2. ## 二、透明状态栏样式,遮盖标题
  3. - 1.效果图
  4. ![iShot2020-08-2623.28.32.gif](https://cdn.nlark.com/yuque/0/2020/gif/1624725/1598455737757-9962fc32-866d-4915-9358-972c202f4fcf.gif#align=left&display=inline&height=480&margin=%5Bobject%20Object%5D&name=iShot2020-08-2623.28.32.gif&originHeight=480&originWidth=290&size=3366414&status=done&style=none&width=290)
  5. - 2. xml

<?xml version=”1.0” encoding=”utf-8”?>

  1. <androidx.coordinatorlayout.widget.CoordinatorLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <com.google.android.material.appbar.AppBarLayout
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:theme="@style/AppTheme.AppBarOverlay"
  8. >
  1. <androidx.appcompat.widget.Toolbar
  2. android:id="@+id/tb_drawer_transpate_toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="@color/blue_74D3FF"
  6. />
  7. </com.google.android.material.appbar.AppBarLayout>
  8. <include layout="@layout/content_scrolling"
  9. />
  10. </androidx.coordinatorlayout.widget.CoordinatorLayout>
  1. <com.google.android.material.navigation.NavigationView
  2. android:id="@+id/nv_drawer_transpate_navigation"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:layout_gravity="start"
  6. app:headerLayout="@layout/drawer_header"
  7. android:background="@color/white"
  8. app:menu="@menu/drawer_menu"/>

  1. - 3. activity

class DrawerTranspateActivity: AppCompatActivity(R.layout.activity_drawer_transpate) { @SuppressLint(“ResourceType”) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

  1. immersionBar {
  2. //fullScreen(true)
  3. transparentStatusBar()
  4. //statusBarColor(R.color.blue_74D3FF)
  5. statusBarDarkFont(false)
  6. }
  7. tb_drawer_transpate_toolbar.title = "抽屉透明状态栏"
  8. setSupportActionBar(tb_drawer_transpate_toolbar)

// tb_drawer_transpate_toolbar.setNavigationIcon(R.mipmap.ic_navigation_menu) // tb_drawer_transpate_toolbar.setNavigationOnClickListener { // dl_drawer_bottom_drawer.open() // }

  1. //创建返回键,并实现打开关/闭监听
  2. val actionBarDrawerToggle = ActionBarDrawerToggle(
  3. this,dl_drawer_transpate_drawer,tb_drawer_transpate_toolbar,
  4. R.string.navigation_drawer_open, R.string.navigation_drawer_close)
  5. dl_drawer_transpate_drawer.addDrawerListener(actionBarDrawerToggle)
  6. actionBarDrawerToggle.syncState()
  7. //抽屉打开关闭监听

// dl_drawer_transpate_drawer.addDrawerListener(object : DrawerLayout.DrawerListener{ // override fun onDrawerStateChanged(newState: Int) { // // } // // override fun onDrawerSlide(drawerView: View, slideOffset: Float) { // // } // // override fun onDrawerClosed(drawerView: View) { // // } // // override fun onDrawerOpened(drawerView: View) { // // } // // })

  1. //侧滑抽屉点击监听
  2. nv_drawer_transpate_navigation.run {
  3. //设置每个文字和图片颜色变化
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  5. itemTextColor = resources.getColorStateList(R.drawable.nav_menu_text_color, null)
  6. itemIconTintList = resources.getColorStateList(R.drawable.nav_menu_text_color, null)
  7. }
  8. //设置默认选中
  9. setCheckedItem(R.id.nav_home)
  10. setNavigationItemSelectedListener {
  11. Snackbar.make(nv_drawer_transpate_navigation, "hello", Snackbar.LENGTH_LONG)
  12. .setAction("Action", null).show()
  13. dl_drawer_transpate_drawer.close()
  14. true
  15. }
  16. }
  17. }

} ```

三、地址和参考

demo
Android Material Design之Toolbar与Palette实践