8

I'm trying to allow the user to open/close the navigation drawer in my app by tapping the action bar title (this is how the current Android Gmail app is set up). At the moment, the user can toggle the drawer by tapping the app/drawer icon or by sliding it in with a left-right swipe. However, the action bar title itself is not clickable. According to the developer docs, clicking the action bar title should "dispatch onOptionsItemSelected to the host Activity with a MenuItem with item ID android.R.id.home" when we use NAVIGATION_MODE_STANDARD but for some reason I can't get the title to behave this way.

I believe the Navigation Drawer itself is fine, but here is how I set up the Action Bar:

private void configureActionBar(CharSequence mTitle) {

    ActionBar actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    actionBar.setIcon(R.drawable.ic_blank);

    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {
                0xFF004700, 0xFF002900
                });

    gd.setCornerRadius(0f);

    actionBar.setBackgroundDrawable(gd);

    // set the title of the action bar using the given title
    actionBar.setTitle(mTitle);

}

Any suggestions would be greatly appreciated!

4

2 回答 2

24

如果您希望通过点击 ActionBar 的图标/标题来打开抽屉,我建议您使用支持库中提供的ActionBarDrawerToggle类( android.support.v4.app.ActionBarDrawerToggle

参考: https ://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

使用示例:
https ://developer.android.com/training/implementing-navigation/nav-drawer.html

诀窍是在 onOptionsItemSelected() 中捕获事件时,您必须将其传递给 ActionBarDrawerToggle,以便它可以处理打开/关闭抽屉请求:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}
于 2013-09-06T15:51:41.370 回答
0

如果在 AndroidManifest.xml 中设置了应用程序主题属性,则会显示图标/标题,如下所示:

    <application
    android:name=".SampleApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

res/values/styles.xml 保存主题声明

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

它通过使用 android.support.v7.app.ActionBarDrawerToggle 工作,同时不推荐使用 support.v4 类。请参阅如何使用 support-v7-appcompat 库

于 2015-05-23T18:51:38.470 回答