DrawerToggle可能不会显示,因为NavigationIcon不可见

时间:2017-01-06 06:42:22

标签: android navigation toggle

错误:

  

DrawerToggle可能不会显示,因为NavigationIcon不可见。   您可能需要致电actionbar.setDisplayHomeAsUpEnabled(true);

    public class Homepage extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener {

    int storeid;
    String storename;
    private RecyclerView recyclerView;
    ProductAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    ArrayList<ProductClass> Products = new ArrayList<>();
    Database database;
    public DrawerLayout mDrawerLayout;
    public ActionBarDrawerToggle mDrawerToggle;
    ActionBar actionBar;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);
        database = new Database();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this, mDrawerLayout,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        assert mDrawerLayout != null;
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

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


        load();

        View hView =  navigationView.getHeaderView(0);
        TextView nav_name = (TextView)hView.findViewById(R.id.textView);
        nav_name.setText(storename);

        Products = database.getstoreproducts(storeid);

        if (Products == null)
            Toast.makeText(Homepage.this, "Store is empty", Toast.LENGTH_LONG).show();
        else {

            recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
            recyclerView.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new ProductAdapter(Products, Homepage.this);
            recyclerView.setAdapter(adapter);

        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    @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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater m = getMenuInflater();
        return true;
    }

    public void load() {

        SharedPreferences sharedPreferences = getSharedPreferences("MyStore", Context.MODE_PRIVATE);
        storename = sharedPreferences.getString("storename", "Default");
        storeid = sharedPreferences.getInt("storeid", -1);

    }


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

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

        if (id == R.id.nav_home) {

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            }

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

            Intent intent = new Intent(Homepage.this, addnewproduct.class);
            startActivity(intent);

        }

        else if (id == R.id.nav_user) {

            Intent intent = new Intent(Homepage.this, storeinfo.class);
            startActivity(intent);

        }

        else if (id == R.id.nav_logout) {

            SharedPreferences sharedPreferences = getSharedPreferences("MyStore", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("logged", "unsuccessful");
            editor.commit();
            Intent intent = new Intent(Homepage.this, MainActivity.class);
            startActivity(intent);

        }

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

        return true;
    }

}
T尝试了我找到的所有东西,但无法找到任何解决方案。请帮忙。

2 个答案:

答案 0 :(得分:0)

您需要为自定义导航图标添加以下行,

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.logo);

答案 1 :(得分:0)

在OnCreate()

中初始化操作栏后添加以下行
actionbar.setDisplayHomeAsUpEnabled(true);

如果不起作用,请尝试:

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);