Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 活动中缺少导航抽屉汉堡图标_Java_Android - Fatal编程技术网

Java 活动中缺少导航抽屉汉堡图标

Java 活动中缺少导航抽屉汉堡图标,java,android,Java,Android,我用片段和活动构建了一个导航抽屉。所有的片段都有图标,抽屉的访问非常顺畅,但活动中没有任何内容。该活动是默认的“主页”,因此访问导航抽屉至关重要。通常不调用toggle.syncState()是解决方案,但在这种情况下失败 主要活动: public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override prot

我用片段和活动构建了一个导航抽屉。所有的片段都有图标,抽屉的访问非常顺畅,但活动中没有任何内容。该活动是默认的“主页”,因此访问导航抽屉至关重要。通常不调用
toggle.syncState()
是解决方案,但在这种情况下失败

主要活动:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    DrawerLayout drawer = 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.setDrawerIndicatorEnabled(true);
    toggle.syncState();

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

    //Sets default fragment
    Intent i = new Intent(MainActivity.this,GarageActivity.class);
    startActivity(i);

    navigationView.setCheckedItem(R.id.nav_garage);
}

//Name in Action bar
public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

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

public class GarageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_garage);
    getSupportActionBar().setTitle("My Garage");
}
}

舱单:

<?xml version="1.0" encoding="utf-8"?>



首先,您需要启用actionbar的主页按钮。然后将汉堡包图标指定给Home按钮,并编写代码在其侦听器中打开抽屉。以下是步骤:

  • 获取汉堡/菜单图标:

    在项目窗口中,右键单击res文件夹并选择新建>矢量资源

    选择材质图标作为资产类型,然后单击图标按钮打开选择图标窗口

    搜索“菜单”并选择菜单图标(图标为3条水平线)。 单击“确定”,然后将文件重命名为“ic_菜单”,然后单击“下一步”将其导入

  • 在actionbar中启用“主页”按钮:

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

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
    
  • 在选项ItemSelected中添加代码方法:

    首先创建DrawerLayout的全局变量,以便可以用其他方法访问它。在onCreate中添加对该变量的引用,并在OnOptions ItemSelected中使用它以打开抽屉。代码如下:

    public class MainActivity extends AppCompatActivity {
    
    private DrawerLayout mDrawerLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    mDrawerLayout = findViewById(R.id.drawer_layout);
    ...
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
           case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
         }
    return super.onOptionsItemSelected(item);
       }
    }
    

  • source

    尝试在此活动生命周期方法中调用
    toggle.syncState
    @Override protected void onPostCreate(@Nullable Bundle savedInstanceState){super.onPostCreate(savedInstanceState);mToggle.syncState();}
    public class MainActivity extends AppCompatActivity {
    
    private DrawerLayout mDrawerLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    mDrawerLayout = findViewById(R.id.drawer_layout);
    ...
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
           case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
         }
    return super.onOptionsItemSelected(item);
       }
    }