【问题标题】:Hamburger icon not showing?汉堡图标不显示?
【发布时间】:2019-03-04 09:40:09
【问题描述】:

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

活动文件

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

【问题讨论】:

    标签: java android hamburger-menu


    【解决方案1】:

    打电话之前

     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
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2021-12-04
      • 2021-04-29
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多