Android.support.v7的工具栏和抽屉布局-如何隐藏汉堡图标

6

我正在实现工具栏和导航抽屉。我正在使用自定义视图自定义我的工具栏。我在我的工具栏中有自己的菜单(汉堡包)图标,用于打开导航抽屉,并在我的菜单图标(汉堡包)上显示徽章计数。因此,我想隐藏默认的汉堡包图标。

我已经尝试过这样做:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);

请帮忙,谢谢。


请给我们展示更多的代码。 - Skizo-ozᴉʞS ツ
请提供您工具栏的截图。 - Psypher
3个回答

18

在您的ActionBarDrawerToggle上调用.setDrawerIndicatorEnabled(false);


我使用工具栏,但我没有ActionBarDrawerToggle实例,那么我该怎么办呢?有任何想法吗? - user1007522
1
@user1007522,您可以通过以下方式获取actionBarToggle实例:toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); - Bunny

1
这对我有效:

这适用于我:

public void setLogo(String imageUrl) {
  ViewGroup toolbarView = (ViewGroup) toolbar.findViewById(R.id.toolbar);

  // set an arbitrary logo so the support library adds an ImageView to the toolbar.
  // without this, there would be no ImageView elements in the toolbar
  // and the following loop would not work
  // `empty_drawable` is a simple rectangle of size 100x100 with a bg color
  // matching that of the toolbar
  // the size really matters because the smaller you set this image, the next logo
  // you set will be of that size. If your arbitrary drawable is of size 1x1, then
  // the next logo you set will be of the same size. At least that is what happens
  // for me when I load an image with Glide.
  // Your drawable should have a background color too. I chose the toolbar's
  // background color. If you don't set a background color, your app would crash on Kitkat.
  toolbar.setLogo(R.drawable.empty_drawable);

  for (int i = 0; i < toolbarView.getChildCount(); i++) {
    if ((toolbarView.getChildAt(i) instanceof ImageView)) {
      ImageView logoView = (ImageView) toolbarView.getChildAt(i);

      if (!TextUtils.isEmpty(imageUrl)) {
        // this way I can download an image and set the logo that way
        Glide.with(MainActivity.this).load(imageUrl).into(logoView);

        // I do this because there is no space between logo and the toolbar title
        logoView.setPadding(0, 0, 30, 0);

        // and this is because of the `else` block
        logoView.setVisibility(View.VISIBLE);
      } else {
        // pass null to `setLogo()` to hide the logo
        logoView.setVisibility(View.GONE);
        logoView.setPadding(0, 0, 0, 0);
      }
      break;
    }
  }
}

这是我的工具栏:
<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:animateLayoutChanges="true"
  app:popupTheme="@style/AppTheme.PopupOverlay"
  app:title="@string/app_name"
  tools:ignore="UnusedAttribute" />

"

android:animateLayoutChanges="true"这部分使得所有这些变得非常好。而tools:ignore="UnusedAttribute"是因为我的应用程序的最小SDK设置为9。

"

0
在你的Activity的onCreate方法中尝试这个:
getActionBar().setIcon(
   new ColorDrawable(getResources().getColor(android.R.color.transparent)));

如果你正在使用AppCompat,请用getSupportActionBar替换getActionBar


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接