-1

我正在尝试构建一个导航抽屉,并且为此使用了 actionbardrawertoggle。我设置了参数并且没有与它们相关的错误。唯一的问题是我在activity_main.xml 中的drawerlayout 的ID 没有被“识别”为参数。我检查了身份证,他们都很好。我试图更改 id 以查看错误的差异,但没有。正如您将看到的,ID 是相同的。我还尝试将 actionbardrawertoggle 更改为带有工具栏的一个,因为我认为参数的顺序可能存在问题,但没有任何改变。id的名字是drawer。你知道我错过了什么吗?谢谢你的帮助

这是我的代码:

//Here is MainActivity:

package zeinfederico.example.tasklead_newversion

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.system.Os.close
import android.system.Os.open
import androidx.appcompat.app.ActionBarDrawerToggle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.toolbar))

        val drawerToggle = ActionBarDrawerToggle(this, drawer , R.string.open, R.string.close)
        drawer.addDrawerListener(drawerToggle)
        drawerToggle.syncState()

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }
} 

//Here is activity_main.xml

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:titleTextColor="@android:color/white"
            android:background="?attr/colorPrimary">
        </androidx.appcompat.widget.Toolbar>
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clipToPadding="false"
        app:menu="@menu/main_menu"
        />

</androidx.drawerlayout.widget.DrawerLayout>```

4

1 回答 1

0

什么是drawer?你还没有在 Java/Kotlin 端初始化这样的值,所以它没有解决......

setContentView把这个放在后面setSupportActionBar

val drawer = findViewById<DrawerLayout>(R.id.drawer)
于 2020-11-09T08:18:06.093 回答