【Android】toolbar & DrawerLayout

findViewById<Toolbar>(R.id.toolbar).let {
    setSupportActionBar(it)
    it.title = getString(R.string.app_title)
    
    setupNavigationDrawer(it)
}

设置应用栏 Set up the app bar

private fun setupNavigationDrawer(toolbar: Toolbar) {
    drawerLayout = findViewById<DrawerLayout>(R.id.drawer_layout).also {
        val toggle =
            ActionBarDrawerToggle(
                this,
                it,
                toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
            )
        it.addDrawerListener(toggle)
        toggle.syncState()
    }

    val navigationView = findViewById<NavigationView>(R.id.nav_view)
    navigationView.setNavigationItemSelectedListener(this)
}

DrawerLayout

DrawerLayout acts as a top-level container for window content that allows for interactive “drawer” views to be pulled out from one or both vertical edges of the window.
DrawerLayout作为窗口内容的顶层容器,允许从窗口的一个或两个垂直边缘拉出交互式“抽屉”视图。

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right (or start/end on platform versions that support layout direction.) Note that you can only have one drawer view for each vertical edge of the window. If your layout configures more than one drawer view per vertical edge of the window, an exception will be thrown at runtime.
抽屉的定位和布局是使用子视图上的’ android:layout_gravity '属性来控制的,对应于你想让抽屉从视图的哪一边出现:左或右(或者在支持布局方向的平台版本上开始/结束)。请注意,窗口的每个垂直边缘只能有一个抽屉视图。如果您的布局为每个窗口的垂直边缘配置了多个抽屉视图,则将在运行时抛出异常。

To use a DrawerLayout,

  1. position your primary content view as the first child with width and height of match_parent and no layout_gravity.
  2. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

要使用drawlayout,

  1. 将主内容视图作为第一个子视图,宽度和高度为match_parent,没有 layout_gravity。
  2. 在主内容视图之后,添加抽屉作为子视图,并适当设置 layout_gravity。抽屉通常使用 match_parent 来设置固定宽度的高度。

添加抽屉式导航栏

你可能感兴趣的:(Android,android)