当我在mainActivity中使用汉堡按钮时,导航抽屉不会打开

时间:2017-11-15 06:53:44

标签: java android xml android-studio

您好我正在尝试在我的MainActivity中实施导航抽屉菜单,它会显示汉堡按钮,但是当我点击它时,菜单没有打开。以下是我的代码:

activity_main.xml中:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context="com.example.user.bottomsidenavigation.MainActivity">

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

</FrameLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    app:headerLayout="@layout/header"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:background="#fff"
    app:itemTextColor="#000"
    app:itemIconTint="#000"
    app:menu="@menu/main_menu"
    android:layout_gravity="start">


</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle mToggle;
NavigationView navigationView;

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

    drawerLayout=(DrawerLayout) findViewById(R.id.drawer);
    mToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView=(NavigationView)findViewById(R.id.navigation_view);
}
}

Styles.xml:

<resources>

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

任何人都请帮助我。提前谢谢......

2 个答案:

答案 0 :(得分:3)

试试这个

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

答案 1 :(得分:2)

您正在设置主页按钮启用。设置禁用。

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

然后初始化抽屉

 private void initNavigationDrawer(Toolbar toolbar) {
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
}