【问题标题】:Clicking on hamburger icon sometimes does not open navigation drawer in android单击汉堡包图标有时不会在 android 中打开导航抽屉
【发布时间】:2018-07-06 11:22:18
【问题描述】:

当点击汉堡图标时,有时无法打开导航抽屉,但有时工作完美。点击汉堡图标时出现问题。帮我解决这个问题。我的代码在这里:-

activity_main.xml :-

<android.support.v4.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
     android:fitsSystemWindows="true"
    app:itemBackground="@drawable/drawer_item"
    android:visibility="gone"
    app:headerLayout="@layout/nav_header_main2"
    app:itemTextColor="@color/colorWhite"
    app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

我的 Kotlin 文件:-

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.navFormula -> {

        }
    }

    drawer_layout.closeDrawer(GravityCompat.START)
    return true
}

【问题讨论】:

    标签: android kotlin navigation-drawer


    【解决方案1】:

    检查您的导航是否正确实例化为休闲

    ...
    setSupportActionBar(toolbar)
    
    val toggle = ActionBarDrawerToggle(
            this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    ...
    

    【讨论】:

      【解决方案2】:

      我建议您在 xml 上创建自己的工具栏, 在你的 activity_main.xml

      <android.support.v4.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"
          android:fitsSystemWindows="true"
          tools:context=".MainActivity">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
          <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/actionBarSize"
              android:background="@color/colorPrimary"
              xmlns:android="http://schemas.android.com/apk/res/android">
      
              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
      
                  <RelativeLayout
                      android:id="@+id/menu_container"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:paddingRight="@dimen/default_margin">
      
                      <ImageView
                          android:id="@+id/menuIcon"
                          android:layout_width="30dp"
                          android:layout_height="wrap_content"
                          android:layout_centerVertical="true"
                          android:contentDescription="@string/app_name"
                          android:src="@drawable/ic_menu_black_24dp"/>
      
                  </RelativeLayout>
      
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerInParent="true"
                      android:textColor="@android:color/white"
                      android:textStyle="bold"
                      android:textAlignment="center"
                      android:layout_marginStart="50dp"
                      android:id="@+id/toolTitle" />
      
              </RelativeLayout>
      
          </android.support.v7.widget.Toolbar>
      
          </RelativeLayout>
      
      
          <android.support.design.widget.NavigationView
              android:id="@+id/nav_view"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:fitsSystemWindows="true"
              app:itemBackground="@drawable/drawer_item"
              android:visibility="gone"
              app:headerLayout="@layout/nav_header_main2"
              app:itemTextColor="@color/colorWhite"
              app:menu="@menu/activity_main_drawer" />
      
      </android.support.v4.widget.DrawerLayout>
      

      然后,在您的活动中,在创建时,放置:

          //set the custom toolbar as the action bar
          setSupportActionBar(toolbar)
      
          //set the NavigationItemListener
          navigationView.setNavigationItemSelectedListener { item ->
              item.isChecked = true
      
              //close drawer when an item is clicked
              drawer.closeDrawers()
              when (item.itemId) {
      
              }
              false
          }
      
          //open drawer when the hamburger icon is clicked
          menuIcon.setOnClickListener {
              drawer.openDrawer(nav_view, true)
          }
      

      【讨论】:

        【解决方案3】:

        使用以下设置,点击汉堡图标后总是会打开导航抽屉:

        override fun onCreate(savedInstanceState: Bundle?) {    
            ...
            // Add Toolbar
            setSupportActionBar(findViewById(R.id.tb_settings))
            getSupportActionBar()!!.setDisplayHomeAsUpEnabled(true)
            getSupportActionBar()!!.setDisplayShowHomeEnabled(true)
            ...
        }
        
        override fun onSupportNavigateUp(): Boolean {
            // Open Navigation Bar
            drawer_layout.openDrawer(nav_view, true)
        
            return true
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-02
          • 2022-08-15
          • 2021-12-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多