简体   繁体   English

汉堡图标未显示在工具栏中

[英]Hamburger icon not displaying in toolbar

I am trying to add a hamburger to my toolbar but nothing is working.我正在尝试在我的工具栏中添加一个汉堡包,但没有任何效果。 I know there are several StackOverflow posts already about this topic, but after trying almost all of the methods mentioned in those posts I still didn't find anything that made the hamburger icon appear.我知道已经有几篇关于这个主题的 StackOverflow 帖子,但是在尝试了这些帖子中提到的几乎所有方法之后,我仍然没有找到任何使汉堡图标出现的东西。 Please let me know if there is something wrong with my code, and if you know how to display the hamburger icon.请让我知道我的代码是否有问题,如果您知道如何显示汉堡包图标。

HomePage.class主页.class

package com.example.movieapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;

public class HomePage extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener {
    private DrawerLayout drawer;

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


        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()) {
            case R.id.nav_home:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new HomeFragment()).commit();
                //                  Intent intent = new Intent(getApplicationContext(),HomePage.class);
                //                startActivity(intent);
                break;
            case R.id.nav_profile:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new ProfileFragment()).commit();
                break;
            case R.id.nav_cart:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new CartFragment()).commit();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

Found out that the problem wasn't in my the.class file but instead it was in the XML file.发现问题不在我的.class 文件中,而是在 XML 文件中。 The toolbar layout was set to light, so the hamburger icon was white and my background was also white.工具栏布局设置为浅色,所以汉堡图标是白色的,我的背景也是白色的。 So it was displaying but it was just not able to seen.所以它正在显示,但它只是无法看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM