Android汉堡图标什么都不做

时间:2016-02-02 22:10:38

标签: android navigation-drawer hamburger-menu

我能够展示"汉堡"我工具栏上的图标,但是当我点击它时,什么都没发生......带抽屉的唯一方法是从左侧滑动..

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrayerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrayerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, test));
    mDrawerList.setOnItemClickListener(new DrawerListClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrayerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
        public void onDrawerClosed(View view){
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle("Close");
            invalidateOptionsMenu();
            mDrawerToggle.syncState();
        }
        public void onDrawerOpened(View drawerView){
            super.onDrawerClosed(drawerView);
            getSupportActionBar().setTitle("Open");
            invalidateOptionsMenu();
            mDrawerToggle.syncState();
        }
    };
    mDrawerToggle.syncState();
    mDrayerLayout.setDrawerListener(mDrawerToggle);
}

这是我的xml文件。我不认为这会有所帮助,但谁知道呢。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/my_toolbar">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

谢谢!

1 个答案:

答案 0 :(得分:1)

ActionBarDrawerToggle使用四参数构造函数时,需要覆盖Activity的{​​{1}}方法,并调用切换的相应方法来触发抽屉的打开和关闭。

onOptionsItemSelected()