Android 汉堡包图标无所事事
Android Hamburger Icon Doing Nothing
我已经能够在我的工具栏上显示 "hamburger" 图标,但是当我点击它时,什么也没有发生...打开我的抽屉的唯一方法是从左边滑动..
这是我的代码:
@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
的四参数构造函数时,需要重写Activity
的onOptionsItemSelected()
方法,调用toggle对应的方法触发抽屉打开和关闭。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
return super.onOptionsItemSelected(item);
}
我已经能够在我的工具栏上显示 "hamburger" 图标,但是当我点击它时,什么也没有发生...打开我的抽屉的唯一方法是从左边滑动..
这是我的代码:
@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
的四参数构造函数时,需要重写Activity
的onOptionsItemSelected()
方法,调用toggle对应的方法触发抽屉打开和关闭。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
return super.onOptionsItemSelected(item);
}