安卓汉堡图标什么都不做



我已经能够在工具栏上显示"汉堡包"图标,但是当我单击它时,什么也没发生......带抽屉的唯一方法是从左边滑动。

这是我的代码:

@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>

谢谢!

当使用 ActionBarDrawerToggle 的四参数构造函数时,需要覆盖ActivityonOptionsItemSelected() 方法,并调用切换的相应方法来触发抽屉的打开和关闭。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    ...
    return super.onOptionsItemSelected(item);
}

相关内容

  • 没有找到相关文章

最新更新