java - 汉堡图标不显示?

标签 java android hamburger-menu

我刚刚开始使用 Android Studio,正在创建一个汉堡包图标,现在一切正常,但汉堡包图标不可见,菜单按预期关闭和打开,可以单击,但图标不可见。任何帮助将不胜感激。

Activity 文件

package com.example.greyhound;

import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout draw ;
    ActionBarDrawerToggle toggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar tool = findViewById(R.id.toolbar);
        setSupportActionBar(tool);
        draw = findViewById(R.id.hamburger);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,draw,tool,R.string.open_menu,R.string.close_menu);
        draw.addDrawerListener(toggle);
        toggle.syncState();
    }

}

布局文件...

<?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/hamburger"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

            <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>   
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/menu_layer"
        app:menu="@menu/burger_menu">
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout>

菜单层

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/colorAccent">

</LinearLayout>

和样式表

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

我尝试添加/删除布局文件,但问题仍然存在。 我假设问题出在样式文件中,所以我将“windowActionBar”设置为 true ,我查看了 here也是如此。

最佳答案

打电话之前

 toggle.syncState();

添加:

 actionBar.setDisplayHomeAsUpEnabled(true); // this sets the button to the back icon
 actionBar.setHomeButtonEnabled(true); // makes it clickable
 actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);// set your own icon

希望这对您有帮助。

关于java - 汉堡图标不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54980395/

相关文章:

java - 检查文件是否在同一天早些时候运行过

java - 如果在多个 EditText 中有输入,如何更改 TextView?

android - 如何更改默认的汉堡包图标?

java - 如何使用 Picasso 从 URL 更改抽屉导航中的汉堡菜单图标?

javascript - 如何通过单击外部来关闭汉堡菜单

java - 支柱 2 s :select tag dynamic id

Java多态: finding the right design pattern

java - 几个小时后无法让我的归并排序工作

android - java.lang.RuntimeException 无法实例化服务 : java. lang.NullPointerException

android - 在 android 上实现类似照片的叠加