【问题标题】:Android navigation drawer 'hamburger' not visibleAndroid 导航抽屉“汉堡包”不可见
【发布时间】:2016-11-08 00:07:19
【问题描述】:

我正在开发一个 Android 应用程序,我在该应用程序中添加了一个新的 Navigation Drawer 活动。即使抽屉出现,汉堡包图标也不会出现。我该怎么做? 见下面的默认代码:

public class NavigationDrawer extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Connect to database", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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.setDrawerListener(toggle);
    toggle.syncState();

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

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.navigation_drawer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

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

}

【问题讨论】:

    标签: android


    【解决方案1】:

    删除这个

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    

    【讨论】:

      【解决方案2】:

      我不确定这是否能解决您的问题,但ActionBarDrawerToggle documentation 说:

      要使用 ActionBarDrawerToggle,请在您的 Activity 中创建一个并调用与您的 Activity 回调对应的以下方法:

      onConfigurationChanged

      onOptionsItemSelected

      在 onRestoreInstanceState 发生后,从您的 Activity 的 onPostCreate 调用 syncState() 以将指示器与链接的 DrawerLayout 的状态同步。

      所以你应该把ActionBarDrawerToggle toggle作为类成员,然后调用:

      • toggle.syncState() 进入 onPostCreate() 方法
      • toggle.onConfigurationChanged() 进入 onConfigurationChanged() 方法
      • toggle.onOptionsItemSelected() 进入 onOptionsItemSelected() 方法

      并且还从您的onCreate() 方法中删除toggle.syncState() 调用。

      public class NavigationDrawer extends AppCompatActivity
              implements NavigationView.OnNavigationItemSelectedListener {
              
          private ActionBarDrawerToggle mToggle;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_navigation_drawer);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
      
              FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
              fab.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Snackbar.make(view, "Connect to database", Snackbar.LENGTH_LONG)
                              .setAction("Action", null).show();
                  }
              });
      
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      
              mToggle = new ActionBarDrawerToggle(
                      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
              drawer.setDrawerListener(mToggle);
      
              NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
              navigationView.setNavigationItemSelectedListener(this);
          }
          
          @Override
          protected void onPostCreate(Bundle savedInstanceState){
              super.onPostCreate(savedInstanceState);
              mToggle.syncState();
          }
          
          @Override
          public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
              mToggle.onConfigurationChanged(newConfig);
          }
      
          @Override
          public void onBackPressed() {
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
              if (drawer.isDrawerOpen(GravityCompat.START)) {
                  drawer.closeDrawer(GravityCompat.START);
              } else {
                  super.onBackPressed();
              }
          }
      
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.navigation_drawer, menu);
              return true;
          }
      
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              mToggle.onOptionsItemSelected(item);
          
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
      
              //noinspection SimplifiableIfStatement
              if (id == R.id.action_settings) {
                  return true;
              }
      
              return super.onOptionsItemSelected(item);
          }
      
          @SuppressWarnings("StatementWithEmptyBody")
          @Override
          public boolean onNavigationItemSelected(MenuItem item) {
              // Handle navigation view item clicks here.
              int id = item.getItemId();
      
              if (id == R.id.nav_camera) {
                  // Handle the camera action
              } else if (id == R.id.nav_gallery) {
      
              } else if (id == R.id.nav_slideshow) {
      
              } else if (id == R.id.nav_manage) {
      
              } else if (id == R.id.nav_share) {
      
              } else if (id == R.id.nav_send) {
      
              }
      
              DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
              drawer.closeDrawer(GravityCompat.START);
              return true;
          }
      }
      

      【讨论】:

        【解决方案3】:

        添加这个

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        

        setSupportActionBar之后

        【讨论】:

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