仅使用标题切换DrawerLayout(没有应用程序图标)?

时间:2013-06-18 05:40:07

标签: android android-actionbar drawerlayout

我有DrawerLayout的活动。我可以通过两种不同的方式打开抽屉......通过从屏幕的左侧区域向右滑动并单击应用程序标题。我不要显示应用图标,只显示标题。我完全按照Google推荐的方式执行了此操作:Creating a Navigation Drawer: Open and Close with the App Icon

就打开和关闭抽屉本身而言,一切都是有用的。但是,它不会显示假设要使用的标准DrawerLayout图标。相反,我得到常规的插入符号(看起来像一个小于符号)。

只要我将应用图标添加回ActionBar,它就会按预期开始工作。抽屉打开或关闭时,抽屉布局图标会显示并设置动画。我已尝试在我的样式XML文件中以编程方式删除应用程序图标。

有没有办法让DrawerLayout图标无法使用应用程序图标???

更新:我找到了解决办法,但这比解决方案更糟糕。我只是创建了1x1像素透明PNG(blank.png)并将其设置为我的styles.xml文件中的app图标。以下是所有相关代码:

styles.xml

<style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyCustomActionBar</item>
    <item name="android:icon">@drawable/blank</item>
</style>

<style name="MyCustomActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>

MainActivity - &gt;的onCreate()

this.navDrawerToggle = new ActionBarDrawerToggle
(
    this,
    this.navDrawerLayout,
    R.drawable.icon_nav_drawer,
    R.string.nav_drawer_open,
    R.string.nav_drawer_closed
)
{
    public void onDrawerClosed(View view) {}
    public void onDrawerOpened(View drawerView) {}
};

MainActivity - &gt; onPostCreate()

super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();

MainActivity - &gt;的onResume()

this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);

MainActivity - &gt;的onPause()

this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);

MainActivity - &gt; onConfigurationChanged(配置newConfig)

super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);

MainActivity - &gt; onOptionsItemSelected(MenuItem item)

if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;}
else
{
    // A bunch of item click handling happens here...

    return true;
}

3 个答案:

答案 0 :(得分:13)

我很好奇这个,所以我尝试了DrawerLayout的样本,它运行良好。此外,似乎你必须使用自己的抽屉图标drawable,无论如何建议。您可以从此站点Creating a Navigation Drawer下载默认资产,并将它们放在各自的可绘制资源文件夹中。

这对我有用:

ActionBar actionBar = getActionBar();

// Let's get rid of the app icon here
actionBar.setIcon(null);
actionBar.setTitle("App Name");

// Setting these 3 options allows us to show the title as well as a "Home" elements
// "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable
// and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE 
        | ActionBar.DISPLAY_SHOW_HOME 
        | ActionBar.DISPLAY_HOME_AS_UP);

// Get a reference of the DrawerLayout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(drawerToggle);

// Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer
// (this will also give you the nice/smooth animation) as well as allow you to do some
// other things depending on the events: onDrawerClosed & onDrawerOpened.
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
          this,                   /* host Activity */
          drawerLayout,           /* DrawerLayout object */
          R.drawable.ic_drawer,   /* nav drawer image to replace 'Up' caret */
          R.string.drawer_open,   /* "open drawer" description for accessibility */
          R.string.drawer_closed  /* "close drawer" description for accessibility */
      ) {
    public void onDrawerClosed(View view) {
        actionBar.setTitle("Closed...");
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    public void onDrawerOpened(View drawerView) {
        actionBar.setTitle("Open...");
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }
};

// Set a listener to be notified of drawer events.
drawerLayout.setDrawerListener(drawerToggle);

UPDATE:似乎没有考虑ActionBar样式的android:displayOptions。改为使用actionBar.setDisplayOptions(int options)。

答案 1 :(得分:0)

设置抽屉切换后,您需要调用以下方法:

 mDrawerToggle.syncState();

所以你的代码看起来像这样:

 mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
            ) {
        public void onDrawerClosed(View view) {
            actionBar.setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }


        public void onDrawerOpened(View drawerView) {
            actionBar.setTitle("Preview Mode");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.syncState();
    mDrawerLayout.setDrawerListener(mDrawerToggle);

答案 2 :(得分:0)

getActionBar().setIcon(android.R.color.transparent);

这对我有用.....;)