6

我有一个活动DrawerLayout。我可以通过两种不同的方式打开抽屉……从屏幕左侧区域向右滑动并单击应用程序标题。我没有显示应用程序图标,只有标题。我已经完全按照谷歌的建议实现了这个:创建导航抽屉:使用应用程序图标打开和关闭

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

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

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

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

样式.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(配置 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;
}
4

3 回答 3

13

我对此很好奇,所以我用 DrawerLayout 的示例进行了尝试,效果很好。此外,您似乎必须使用自己的可绘制抽屉图标,无论如何都建议您这样做。您可以从该站点下载默认资源创建导航抽屉并将它们放在各自的可绘制资源文件夹中。

这对我有用:

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

于 2013-06-18T15:56:28.063 回答
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);
于 2013-10-03T15:18:05.997 回答
0

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

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

于 2015-09-17T06:45:16.503 回答