【问题标题】:android.support.v7 Toolbar & DrawerLayout- how to hide Hamburger iconandroid.support.v7 Toolbar & DrawerLayout-如何隐藏汉堡图标
【发布时间】:2023-04-04 19:14:01
【问题描述】:

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

我试过这样:

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

请帮助我。在此先感谢。

【问题讨论】:

  • 请向我们展示更多代码
  • 请也给你的工具栏截图

标签: android navigation-drawer drawerlayout android-toolbar drawertoggle


【解决方案1】:

在 ActionBarDrawerToggle 上调用 .setDrawerIndicatorEnabled(false);

【讨论】:

  • 我使用了工具栏,但我没有 actionbardrawertoggle 实例,那我该怎么做呢?有什么想法吗?
  • @user1007522 使用可以通过这种方式获取 actionBarToggle 实例:toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
【解决方案2】:

这对我有用:

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。

【讨论】:

    【解决方案3】:

    在你的活动的 onCreate 中试试这个:

    getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    

    如果您使用的是 AppCompat,请将 getActionBar 替换为 getSupportActionBar

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2018-11-09
      • 1970-01-01
      • 2019-10-30
      • 2021-11-29
      相关资源
      最近更新 更多