没有带导航抽屉的汉堡工具栏

时间:2021-04-07 15:53:57

标签: android kotlin navigation-drawer

您好,当我在模拟器上运行带有抽屉菜单的 Android 应用程序 (Kotlin) 时收到此消息:

W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);

我的顶部酒吧/汉堡不可见(但如果你抓住它,菜单就可以工作)。

这是我的活动代码:

package nameofthepackage

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import kotlinx.android.synthetic.main.activity_hub.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


const val BASE_URL = "https://xxxxxxxxxx"

class HubActivity : AppCompatActivity() {

    private var TAG = "HubActivity"
    lateinit var toggle: ActionBarDrawerToggle

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hub)

        toggle = ActionBarDrawerToggle(this, drawerlayout, R.string.open, R.string.close)
        drawerlayout.addDrawerListener(toggle)
        toggle.syncState()

        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        navigationView.setNavigationItemSelectedListener {
            when(it.itemId){
                .... other options for items ...

.... other functions ....




    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(toggle.onOptionsItemSelected(item)){
            return true
        }
        return super.onOptionsItemSelected(item)
    }



..... other functions ....

}

这是我的抽屉菜单代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/myEvents"
        android:icon="@android:drawable/ic_menu_myplaces"
        android:title="My events" />

    <item
        .... other items ...

</menu>

这是我的 nav_header 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:alwaysDrawnWithCache="false"
    android:background="@color/colorPrimary"
    android:backgroundTint="#005F6F"
    android:visibility="visible"
    tools:visibility="visible" />

还有我的活动布局:

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#9CC8CF">

        <TextView
            android:id="@+id/HubActivity_Central_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test test test"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_drawer_menu"
        android:fitsSystemWindows="true"/>

</androidx.drawerlayout.widget.DrawerLayout>

我没有找到任何解决方案,一切似乎都符合本教程(https://www.youtube.com/watch?v=do4vb0MdLFY&t=741s)但无法显示汉堡工具栏...

感谢您的帮助

PS : nav_drawer_menu 的预览: enter image description here

0 个答案:

没有答案