android.view.MenuItem.getIcon()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中android.view.MenuItem.getIcon()方法的一些代码示例,展示了MenuItem.getIcon()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MenuItem.getIcon()方法的具体详情如下:
包路径:android.view.MenuItem
类名称:MenuItem
方法名:getIcon

暂无

代码示例

代码示例来源:origin: stackoverflow.com

public boolean onCreateOptionsMenu(Menu menu) {
  //inflate menu
  getMenuInflater().inflate(R.menu.menu_my, menu);

  // Get the notifications MenuItem and LayerDrawable (layer-list)
  MenuItem item = menu.findItem(R.id.action_notifications);
  LayerDrawable icon = (LayerDrawable) item.getIcon();

  // Update LayerDrawable's BadgeDrawable
  Utils2.setBadgeCount(this, icon, 2);

  return true;
}

代码示例来源:origin: stackoverflow.com

MenuItem favoriteItem = menu.findItem(R.id.action_favorite);
Drawable newIcon = (Drawable)favoriteItem.getIcon();
newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);
favoriteItem.setIcon(newIcon);

代码示例来源:origin: ArthurHub/Android-Image-Cropper

/** Update the color of a specific menu item to the given color. */
private void updateMenuItemIconColor(Menu menu, int itemId, int color) {
 MenuItem menuItem = menu.findItem(itemId);
 if (menuItem != null) {
  Drawable menuItemIcon = menuItem.getIcon();
  if (menuItemIcon != null) {
   try {
    menuItemIcon.mutate();
    menuItemIcon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    menuItem.setIcon(menuItemIcon);
   } catch (Exception e) {
    Log.w("AIC", "Failed to update menu item color", e);
   }
  }
 }
}
// endregion

代码示例来源:origin: Yalantis/uCrop

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
  getMenuInflater().inflate(R.menu.ucrop_menu_activity, menu);
  // Change crop & loader menu icons color to match the rest of the UI colors
  MenuItem menuItemLoader = menu.findItem(R.id.menu_loader);
  Drawable menuItemLoaderIcon = menuItemLoader.getIcon();
  if (menuItemLoaderIcon != null) {
    try {
      menuItemLoaderIcon.mutate();
      menuItemLoaderIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
      menuItemLoader.setIcon(menuItemLoaderIcon);
    } catch (IllegalStateException e) {
      Log.i(TAG, String.format("%s - %s", e.getMessage(), getString(R.string.ucrop_mutate_exception_hint)));
    }
    ((Animatable) menuItemLoader.getIcon()).start();
  }
  MenuItem menuItemCrop = menu.findItem(R.id.menu_crop);
  Drawable menuItemCropIcon = ContextCompat.getDrawable(this, mToolbarCropDrawable);
  if (menuItemCropIcon != null) {
    menuItemCropIcon.mutate();
    menuItemCropIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
    menuItemCrop.setIcon(menuItemCropIcon);
  }
  return true;
}

代码示例来源:origin: hidroh/materialistic

@SuppressWarnings("unused")
  public void setIcon(MenuItem item, @DrawableRes int icon) {
    item.setIcon(icon);
    Drawable drawable = item.getIcon();
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, mTextColorPrimary);
  }
}

代码示例来源:origin: Flipboard/bottomsheet

public void bindView(SheetMenuItem item) {
    icon.setImageDrawable(item.getMenuItem().getIcon());
    label.setText(item.getMenuItem().getTitle());
  }
}

代码示例来源:origin: MindorksOpenSource/android-mvp-architecture

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  Drawable drawable = item.getIcon();
  if (drawable instanceof Animatable) {
    ((Animatable) drawable).start();
  }
  switch (item.getItemId()) {
    case R.id.action_cut:
      return true;
    case R.id.action_copy:
      return true;
    case R.id.action_share:
      return true;
    case R.id.action_delete:
      return true;
    default:
      return super.onOptionsItemSelected(item);
  }
}

代码示例来源:origin: square/assertj-android

public MenuItemAssert hasIcon(Drawable icon) {
 isNotNull();
 Drawable actualIcon = actual.getIcon();
 assertThat(actualIcon) //
   .overridingErrorMessage("Expected icon <%s> but was <%s>.", icon, actualIcon) //
   .isSameAs(icon);
 return this;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void setIcon_shouldNullifyOnZero() throws Exception {
 assertThat(item.getIcon()).isNull();
 item.setIcon(R.drawable.an_image);
 assertThat(shadowOf(item.getIcon()).getCreatedFromResId()).isEqualTo(R.drawable.an_image);
 item.setIcon(0);
 assertThat(item.getIcon()).isNull();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getIcon_shouldReturnDrawableFromSetIconDrawable() throws Exception {
 Drawable testDrawable =
   ApplicationProvider.getApplicationContext().getResources().getDrawable(R.drawable.an_image);
 assertThat(testDrawable).isNotNull();
 assertThat(item.getIcon()).isNull();
 item.setIcon(testDrawable);
 assertThat(item.getIcon()).isSameAs(testDrawable);
}

代码示例来源:origin: aurelhubert/ahbottomnavigation

/**
 * Setup bottom navigation (with colors)
 *
 * @param ahBottomNavigation AHBottomNavigation: Bottom navigation
 * @param colors             int[]: Colors of the item
 */
public void setupWithBottomNavigation(AHBottomNavigation ahBottomNavigation, @ColorInt int[] colors) {
  if (navigationItems == null) {
    navigationItems = new ArrayList<>();
  } else {
    navigationItems.clear();
  }
  if (mMenu != null) {
    for (int i = 0; i < mMenu.size(); i++) {
      MenuItem item = mMenu.getItem(i);
      if (colors != null && colors.length >= mMenu.size() && colors[i] != 0) {
        AHBottomNavigationItem navigationItem = new AHBottomNavigationItem(String.valueOf(item.getTitle()), item.getIcon(), colors[i]);
        navigationItems.add(navigationItem);
      } else {
        AHBottomNavigationItem navigationItem = new AHBottomNavigationItem(String.valueOf(item.getTitle()), item.getIcon());
        navigationItems.add(navigationItem);
      }
    }
    ahBottomNavigation.removeAllItems();
    ahBottomNavigation.addItems(navigationItems);
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void getIcon_shouldReturnDrawableFromSetIconResourceId() throws Exception {
 assertThat(item.getIcon()).isNull();
 item.setIcon(R.drawable.an_other_image);
 assertThat(shadowOf(item.getIcon()).getCreatedFromResId()).isEqualTo(R.drawable.an_other_image);
}

代码示例来源:origin: k9mail/k-9

prev.getIcon().setAlpha(canDoPrev ? 255 : 127);
next.getIcon().setAlpha(canDoNext ? 255 : 127);

代码示例来源:origin: ankidroid/Anki-Android

menu.findItem(R.id.action_undo).setEnabled(true).getIcon().setAlpha(Themes.ALPHA_ICON_ENABLED_LIGHT);
} else if (mShowWhiteboard && mWhiteboard != null && mWhiteboard.isUndoModeActive()) {
  menu.findItem(R.id.action_undo).setEnabled(false).getIcon().setAlpha(Themes.ALPHA_ICON_DISABLED_LIGHT);
} else if (colIsOpen() && getCol().undoAvailable()) {
  menu.findItem(R.id.action_undo).setIcon(R.drawable.ic_undo_white_24dp);
  menu.findItem(R.id.action_undo).setEnabled(true).getIcon().setAlpha(Themes.ALPHA_ICON_ENABLED_LIGHT);
} else {
  menu.findItem(R.id.action_undo).setIcon(R.drawable.ic_undo_white_24dp);
  menu.findItem(R.id.action_undo).setEnabled(false).getIcon().setAlpha(Themes.ALPHA_ICON_DISABLED_LIGHT);

代码示例来源:origin: hidroh/materialistic

/**
 * Callback that should be triggered after menu has been inflated
 * @param menu    inflated menu
 */
public void onOptionsMenuCreated(Menu menu) {
  for (int i = 0; i < menu.size(); i++) {
    Drawable drawable = menu.getItem(i).getIcon();
    if (drawable == null) {
      continue;
    }
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, mTextColorPrimary);
  }
}

代码示例来源:origin: ZieIony/Carbon

public static Menu getMenu(Context context, android.view.Menu baseMenu) {
  Context contextWrapper = CarbonContextWrapper.wrap(context);
  Menu menu = new Menu(contextWrapper);
  for (int i = 0; i < baseMenu.size(); i++) {
    android.view.MenuItem menuItem = baseMenu.getItem(i);
    menu.add(menuItem.getGroupId(), menuItem.getItemId(), menuItem.getOrder(), menuItem.getTitle()).setIcon(menuItem.getIcon()).setVisible(menuItem.isVisible()).setEnabled(menuItem.isEnabled());
  }
  return menu;
}

代码示例来源:origin: arimorty/floatingsearchview

ImageView action = (ImageView) currentView;
final MenuItem actionItem = mActionItems.get(i);
action.setImageDrawable(actionItem.getIcon());
Util.setIconColor(action, mActionIconColor);
action.setOnClickListener(new OnClickListener() {

代码示例来源:origin: mikepenz/MaterialDrawer

iDrawerItem = new PrimaryDrawerItem()
    .withName(mMenuItem.getTitle().toString())
    .withIcon(mMenuItem.getIcon())
    .withIdentifier(mMenuItem.getItemId())
    .withEnabled(mMenuItem.isEnabled())
iDrawerItem = new SecondaryDrawerItem()
    .withName(mMenuItem.getTitle().toString())
    .withIcon(mMenuItem.getIcon())
    .withIdentifier(mMenuItem.getItemId())
    .withEnabled(mMenuItem.isEnabled());
iDrawerItem = new PrimaryDrawerItem()
    .withName(mMenuItem.getTitle().toString())
    .withIcon(mMenuItem.getIcon())
    .withIdentifier(mMenuItem.getItemId())
    .withEnabled(mMenuItem.isEnabled());

代码示例来源:origin: ZieIony/Carbon

public void setMenu(Menu menu) {
  this.menu = menu;
  content.removeAllViews();
  content.setWeightSum(menu.size());
  for (int i = 0; i < menu.size(); i++) {
    final MenuItem item = menu.getItem(i);
    final View view = View.inflate(getContext(), R.layout.carbon_bottombar_item, null);
    view.setOnClickListener(v -> {
      if (view == activeView)
        return;
      if (activeView != null) {
        deselectItem(activeView);
      }
      selectItem(view);
      if (listener != null)
        listener.onMenuItemClick(item);
    });
    ImageView icon = view.findViewById(R.id.carbon_bottomIcon);
    icon.setTintList(new DefaultColorStateList(getContext()));
    icon.setImageDrawable(item.getIcon());
    TextView text = view.findViewById(R.id.carbon_bottomText);
    text.setTextColor(new DefaultColorStateList(getContext()));
    text.setText(item.getTitle());
    content.addView(view, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
  }
}

代码示例来源:origin: arimorty/floatingsearchview

@Override
public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
  if (subMenu.hasVisibleItems()) {
    MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu, mAnchorView);
    subPopup.setCallback(mPresenterCallback);
    boolean preserveIconSpacing = false;
    final int count = subMenu.size();
    for (int i = 0; i < count; i++) {
      MenuItem childItem = subMenu.getItem(i);
      if (childItem.isVisible() && childItem.getIcon() != null) {
        preserveIconSpacing = true;
        break;
      }
    }
    subPopup.setForceShowIcon(preserveIconSpacing);
    if (subPopup.tryShow()) {
      if (mPresenterCallback != null) {
        mPresenterCallback.onOpenSubMenu(subMenu);
      }
      return true;
    }
  }
  return false;
}
@Override

相关文章