【问题标题】:How can i assign code to drawer button?如何将代码分配给抽屉按钮?
【发布时间】:2017-12-29 11:09:20
【问题描述】:

我希望在单击导航菜单中的按钮以启动新活动时。使用当前代码,单击按钮时不会发生任何事情。我是 Android 新手,所以如果我不理解您的代码并提出一些问题,请不要生气。

这是我的 navigation_menu.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="Home"
    android:icon="@mipmap/ic_home_black_24dp">
</item>
<item android:id="@+id/nav_delete"
    android:title="Delete Boards"
    android:icon="@mipmap/ic_close_black_24dp">
</item>
<item android:id="@+id/nav_settings"
    android:title="Settings"
    android:icon="@mipmap/ic_settings_black_24dp">
</item>

这是我的 ma​​inActivity.java 抽屉代码:

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

 mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mToggle.onOptionsItemSelected(item)){

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.nav_delete) {
            //call new activity
        }
}

    return super.onOptionsItemSelected(item);
}

}

MainActivity.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.studios.cookie.chanomatic.MainActivity"
android:id="@+id/drawerLayout">

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.02"
        android:text="Boards"
        android:textAlignment="center"
        android:textColor="@android:color/black" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id = "@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 你可以从这里找到你的答案*.com/questions/30886453/…
  • @SonuSanjeev 这就是我想弄清楚的...
  • 截图:ibb.co/hxuQew
  • @MassiGamingRo 试试我的答案
  • @MassiGamingRo 检查我的回答你应该在点击项目时关闭抽屉

标签: android button navigation-drawer


【解决方案1】:

在您的onCreate 方法中添加以下代码:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

在你的 mainactivity 类中实现列表器:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

而XML文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

然后在你的 onNavigationItemSelected 方法中看起来像这样:

   @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_delete) {
        //call new activity
         }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
     }

希望对你有帮助

【讨论】:

    【解决方案2】:

    你给导航菜单充气了吗? 如果没有,请确保您的导航菜单位于 res/menu 文件夹中,然后添加:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.navigation_menu, menu);
        return true;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      @SuppressWarnings("StatementWithEmptyBody")
      @Override
          public boolean onNavigationItemSelected(MenuItem item)
          {
              // Handle navigation view item clicks here.
              int id = item.getItemId();
      
              if (id == R.id.nav_account)
              {
                    //your intent
              }
      
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
              drawer.closeDrawer(GravityCompat.START);
              return true;
         }
      

      在主活动中实现导航菜单的监听器。

      public class MainActivity extends AppCompatActivity
              implements NavigationView.OnNavigationItemSelectedListener
      

      在活动中找到导航视图引用并设置监听器。

      navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
      

      完整代码:

      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
      mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
      
      mDrawerLayout.addDrawerListener(mToggle);
      mToggle.syncState();
      navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
      

      XML 文件:

      <?xml version="1.0" encoding="utf-8"?>
      <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">
      
          <include
              layout="@layout/app_bar_main"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
      
          <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:itemIconTint="@drawable/drawer_icon"
              app:itemBackground="@drawable/drawer_text_background"
              app:itemTextColor="@drawable/drawer_text"
              app:headerLayout="@layout/nav_header_main"
              app:menu="@menu/activity_main_drawer" />
      
      </android.support.v4.widget.DrawerLayout>
      

      【讨论】:

      • 当我输入代码时,它说 方法不会覆盖它的超类中的方法
      • @MassiGamingRo 您需要为导航菜单实现监听器。 implements NavigationView.OnNavigationItemSelectedListener
      • 我应该把 navigationView.setNavigationItemSelectedListener(this);
      • @MassiGamingRo 在mToggle.syncState(); 这行之后
      • 您的代码非常有用,我确信它可以工作,但我没有任何 nav_view...
      最近更新 更多