现在的方法是使用导航架构组件 https://developer.android.com/guide/navigation/navigation-getting-started为了在您的应用程序中拥有单个活动和多个片段;每个屏幕都可以用一个片段来表示...这是Android studio的默认设置导航抽屉活动模板。
这NavController
用于控制此方法中片段之间的导航
但如果你愿意你可以使用DrawerLayout
不使用NavController
..但在最近的 Android Studio 版本中,没有相应的模板,您必须手动创建它,并手动处理导航、返回堆栈等几乎所有内容。
Example
这是一个轻量级的示例,可以让您开始一个更大的示例。
当您从抽屉中选择一个项目时,通常您可以在抽屉中进行碎片交易NavigationItemSelectedListener
但在下面的例子中我只显示了一个Toast
活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Setting custom ActionBar
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
// Showing the burger button on the ActionBar
supportActionBar?.setDisplayHomeAsUpEnabled(true);
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val toggle =
ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
// Handle navigation click events
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_account -> {
Toast.makeText(this, "Account", Toast.LENGTH_SHORT).show()
}
R.id.nav_settings -> {
Toast.makeText(this, "Setting", Toast.LENGTH_SHORT).show()
}
R.id.nav_logout -> {
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show()
}
}
// Closing navigation drawer
drawerLayout.closeDrawer(GravityCompat.START)
true
}
}
}
活动主文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<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"
app:headerLayout="@layout/navigation_header_layout"
app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
导航_标题_布局.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:layout_centerInParent="true"
android:background="@color/colorPrimary"
android:text="Navigation drawer"
android:textColor="#ffffff"
android:textSize="24sp" />
导航菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_account"
android:title="My Account" />
<item
android:id="@+id/nav_settings"
android:title="Settings" />
<item
android:id="@+id/nav_logout"
android:title="Log Out" />
</menu>
构建.gradle(模块)
implementation 'com.google.android.material:material:1.2.1'
字符串.xml
<resources>
<string name="app_name">Navigation Drawer Example</string>
<string name="open">Open</string>
<string name="close">Close</string>
</resources>
styles.xml(NoActionBar 主题)
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>