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

Jab*_*ari 6 android android-actionbar drawerlayout

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

就打开和关闭抽屉本身而言,一切都是有用的.但是,它不显示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>
Run Code Online (Sandbox Code Playgroud)

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) {}
};
Run Code Online (Sandbox Code Playgroud)

MainActivity - > onPostCreate()

super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();
Run Code Online (Sandbox Code Playgroud)

MainActivity - > onResume()

this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);
Run Code Online (Sandbox Code Playgroud)

MainActivity - > onPause()

this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);
Run Code Online (Sandbox Code Playgroud)

MainActivity - > onConfigurationChanged(配置newConfig)

super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);
Run Code Online (Sandbox Code Playgroud)

MainActivity - > onOptionsItemSelected(MenuItem item)

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

    return true;
}
Run Code Online (Sandbox Code Playgroud)

vel*_*cod 13

我很好奇这个,所以我尝试了DrawerLayout的样本,它工作得很好.此外,似乎你必须使用自己的抽屉图标drawable,无论如何建议.您可以从此站点下载默认资源创建导航抽屉并将它们放在各自的可绘制资源文件夹中.

这对我有用:

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);
Run Code Online (Sandbox Code Playgroud)

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


kar*_*ran 0

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

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