如何使用 Navigation Drawer Activity Google 模板将图标添加到工具栏?

How to add an Icon to the toolbar using Navigation Drawer Activity Google template?

我正在使用“导航抽屉 Activity”模板开发 Android 应用程序。

我想在工具栏(标题左侧)中添加一个 icon

当我使用setIcon时,它在工具栏中央显示图标,但我看不到标题。

我怎样才能正确地做到这一点?

app_bar_main.xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.BodetTag.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.BodetTag.PopupOverlay" />
    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity:

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        //supportActionBar?.setIcon(R.drawable.bodet_icon)

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

谢谢!

对于那些想要替换 burger-menu-icon 的用户,请使用 app:navigationIcon="@drawable/ic_your_icon"。 在您的情况下,我相信这会与 android 导航组件逻辑发生冲突。

所以很可能您想添加一个额外的徽标: 为此,您可以记住工具栏只是一个标准布局并执行某项操作。像这样:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >

          <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/your_icon"
            />
          
          <TextView
            android:id="@+id/toolbarTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="your title here"
            />
            
       </LinearLayout>
    </androidx.appcompat.widget.Toolbar>

这将导致您的标题被破坏。 要解决此问题,我建议禁用 default-toolbar-title:

supportActionBar?.setDisplayShowTitleEnabled(false)

并自行更新您的自定义标题,例如用

navController.addOnDestinationChangedListener { _, destination, _ ->
    toolbarTitle.text = destination.label
}