我刚从Android Studio开始,并且I'am创建了一个汉堡图标,现在一切都很好,但是汉堡偶像不可见,菜单像预期一样关闭并打开图标不可见。任何帮助将不胜感激。
活动文件
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,我也在这里查看。
在调用
之前 toggle.syncState();
add:
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
希望这对您有帮助。