【问题标题】:Toggle DrawerLayout With Title Only (NO App Icon)?仅使用标题切换 DrawerLayout(无应用程序图标)?
【发布时间】:2013-06-18 05:40:07
【问题描述】:

我有一个DrawerLayout 的活动。我可以通过两种不同的方式打开抽屉……从屏幕左侧区域向右滑动并单击应用程序标题。我显示了一个应用程序图标,只有一个标题。我已经完全按照谷歌的建议实现了这个:Creating a Navigation Drawer: Open and Close with the App Icon

只要打开和关闭抽屉本身,一切都可以正常工作。但是,它不显示假定使用的标准DrawerLayout 图标。相反,我得到了常规的向上插入符号(看起来像一个小于号)。

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

有没有办法让DrawerLayout 图标在没有应用程序图标的情况下工作???

更新:我找到了一种解决方法,但它更像是一种 hack,而不是一种解决方案。我只是创建了一个 1x1 像素的透明 PNG (blank.png) 并将其设置为我的 styles.xml 文件中的应用程序图标。以下是所有相关代码:

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 -> 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 -> onPostCreate()

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

MainActivity -> onResume()

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

MainActivity -> onPause()

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

MainActivity -> onConfigurationChanged(Configuration newConfig)

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

MainActivity -> onOptionsItemSelected(MenuItem item)

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

    return true;
}

【问题讨论】:

  • 作为旁注,下次请添加一些代码以了解您正在使用的内容。
  • 在下面查看我的更新。使用 setDisplayOptions 而不是尝试在样式上设置它们可以修复它。

标签: android android-actionbar drawerlayout


【解决方案1】:

我对此很好奇,所以我用 DrawerLayout 的示例进行了尝试,效果很好。此外,您似乎必须使用自己的可绘制抽屉图标,无论如何都建议您这样做。您可以从本站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);

更新: 似乎没有考虑到 ActionBar 样式上的 android:displayOptions。请改用 actionBar.setDisplayOptions(int options)。

【讨论】:

  • 您上面列出的几乎与我实现的完全一样,除了两件事。 1) 我没有使用actionbar.setIcon(null)。相反,我使用了actionbar.setDisplayShowHomeEnabled(false)。 2) 我最终放弃了以编程方式更改 ActionBar 并改为在我的 styles.xml 文件中更改它们,包括显示选项。我要添加actionbar.setIcon(null) 看看会发生什么...
  • setDisplayShowHomeEnabled(false) 与没有选项 ActionBar.DISPLAY_SHOW_HOME 大致相同。您需要 setDisplayShowHomeEnabled(true) 或设置选项。
  • 仍然对我不起作用。你在使用 ActionBarSherlock 吗(我没有)。我也在运行 4.2.2。我已经删除了setDisplayShowHomeEnabled(false)。我找到了一个临时解决方案...我只是创建了一个 1x1 px 透明 PNG 并将其用作我的应用程序图标(通过 styles.xml 文件)。感觉就像是黑客攻击。
  • OP 用代码更新。此外,明天在 DrawLayout 上还有 Google Developers Live Hangout。我在 cmets 中发布了我的问题,其中一位开发人员认为这可能是一个错误......
  • i.imgur.com/CSlC5q7.png 去掉 Actionbar 样式中的“android:displayOptions”,使用 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
【解决方案2】:

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

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

【讨论】:

  • 当我发布此内容时(几个版本的 Android 之前),这是我尝试的第一件事。它在功能上工作,但图标仍然占用相同的空间。现在不是这样吗?如果没有,我会在测试后将此作为新接受的答案。
  • 你说的是哪个版本。我在 5.1 (lolipop) 和 4.4.2 (kit kat) 上进行了测试,两者都运行良好。
  • 这篇文章已经有 2 年的历史了,我试图保持向后兼容,所以冰淇淋三明治和姜饼(我不关心蜂窝)。已接受答案!
【解决方案3】:

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

 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);

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2021-12-28
    • 2014-06-11
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多