android.view.Menu.size()方法的使用及代码示例

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

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

暂无

代码示例

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

/**
   * Get position by menu id
   *
   * @param menuId
   * @return
   */
  public Integer getPositionByMenuId(int menuId) {
    for (int i = 0; i < mMenu.size(); i++) {
      if (mMenu.getItem(i).getItemId() == menuId)
        return i;
    }
    return null;
  }
}

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

public MenuAssert hasSize(int size) {
  isNotNull();
  int actualSize = actual.size();
  Assertions.assertThat(actualSize) //
    .overridingErrorMessage("Expected size <%s> but was <%s>.", size, actualSize) //
    .isEqualTo(size);
  return this;
 }
}

代码示例来源:origin: ittianyu/BottomNavigationViewEx

/**
 * get menu item position in menu
 *
 * @param item
 * @return position if success, -1 otherwise
 */
public int getMenuItemPosition(MenuItem item) {
  // get item id
  int itemId = item.getItemId();
  // get meunu
  Menu menu = getMenu();
  int size = menu.size();
  for (int i = 0; i < size; i++) {
    if (menu.getItem(i).getItemId() == itemId) {
      return i;
    }
  }
  return -1;
}

代码示例来源:origin: ittianyu/BottomNavigationViewEx

MyOnNavigationItemSelectedListener(ViewPager viewPager, BottomNavigationViewEx bnve, boolean smoothScroll, OnNavigationItemSelectedListener listener) {
  this.viewPagerRef = new WeakReference<>(viewPager);
  this.listener = listener;
  this.smoothScroll = smoothScroll;
  // create items
  Menu menu = bnve.getMenu();
  int size = menu.size();
  items = new SparseIntArray(size);
  for (int i = 0; i < size; i++) {
    int itemId = menu.getItem(i).getItemId();
    items.put(itemId, i);
  }
}

代码示例来源:origin: rey5137/material

/**
 * This function should be called in onPrepareOptionsMenu(Menu) of Activity that use
 * Toolbar as ActionBar, or after inflating menu.
 */
public void onPrepareMenu(){
  if(mGroupChanged || mMenuDataChanged){
    mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
    Menu menu = mToolbar.getMenu();
    for(int i = 0, count = menu.size(); i < count; i++){
      MenuItem item = menu.getItem(i);
      item.setVisible(item.getGroupId() == mCurrentGroup || item.getGroupId() == 0);
    }
    mMenuDataChanged = false;
  }
}

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

/** Sets selected navigation drawer item */
protected void selectNavigationItem(int itemId) {
  if (mNavigationView == null) {
    Timber.e("Could not select item in navigation drawer as NavigationView null");
    return;
  }
  Menu menu = mNavigationView.getMenu();
  if (itemId == -1) {
    for (int i = 0; i < menu.size(); i++) {
      menu.getItem(i).setChecked(false);
    }
  } else {
    MenuItem item = menu.findItem(itemId);
    if (item != null) {
      item.setChecked(true);
    } else {
      Timber.e("Could not find item %d", itemId);
    }
  }
}

代码示例来源: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: 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: Flipboard/bottomsheet

for (int i = 0; i < menu.size(); i++) {
  MenuItem item = menu.getItem(i);
  if (item.isVisible()) {
        if (menuType == LIST && i != menu.size() - 1) {
          items.add(SheetMenuItem.SEPARATOR);

代码示例来源: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: facebook/facebook-android-sdk

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
  // only add the menu when the selection fragment is showing
  if (fragments[RPS].isVisible()) {
    if (menu.size() == 0) {
      share = menu.add(R.string.share_on_facebook);
      message = menu.add(R.string.send_with_messenger);
      challenge = menu.add(R.string.challenge_friends);
      settings = menu.add(R.string.check_settings);
    }
    return true;
  } else {
    menu.clear();
    settings = null;
  }
  return false;
}

代码示例来源:origin: code-mc/material-icon-lib

private void populateIcons(Menu menu, IconData root, int defaultIconColor) {
  for(int i = 0; i < menu.size(); i++){
    MenuItem m = menu.getItem(i);
    IconData d = root.children.get(i);
    if(m.hasSubMenu()){
      populateIcons(m.getSubMenu(), d, defaultIconColor);
    }
    if(d.itemIconResId >= 0)
      m.setIcon(
          MaterialDrawableBuilder.with(mContext)
              .setIcon(MaterialDrawableBuilder.IconValue.values()[d.itemIconResId])
              .setColor(d.itemColor != -1 ? d.itemColor : defaultIconColor)
              .setToActionbarSize()
              .build()
      );
  }
}

代码示例来源: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: mikepenz/MaterialDrawer

for (int i = 0; i < mMenu.size(); i++) {
  MenuItem mMenuItem = mMenu.getItem(i);
  IDrawerItem iDrawerItem;

代码示例来源:origin: fossasia/pslab-android

private void unCheckAllMenuItems(Menu menu) {
  int size = menu.size();
  for (int i = 0; i < size; i++) {
    final MenuItem item = menu.getItem(i);
    item.setChecked(false);
  }
}

代码示例来源:origin: fossasia/pslab-android

private void setUpMenu(Menu menu) {
  if (playingData || viewingData) {
    for (int i = 0; i < menu.size(); i++) {
      menu.getItem(i).setVisible(false);
    }
  }
  menu.findItem(R.id.save_graph).setVisible(viewingData || playingData);
  menu.findItem(R.id.play_data).setVisible(viewingData || playingData);
  menu.findItem(R.id.settings).setTitle(getSensorName() + " Configurations");
  menu.findItem(R.id.stop_data).setVisible(viewingData).setEnabled(startedPlay);
}

代码示例来源:origin: willowtreeapps/Hyperion-Android

public static Collection<ViewAttribute> createMenuAttributes(@NonNull Context context, @NonNull Menu menu) {
  final Resources res = context.getResources();
  List<ViewAttribute> attributes = new ArrayList<>();
  attributes.add(new ViewAttribute<>("MenuSize", menu.size()));
  attributes.add(new ViewAttribute<>("MenuHasVisibleItems", menu.hasVisibleItems()));
  for (int i = 0; i < menu.size(); i++) {
    MenuItem item = menu.getItem(i);
    String prefix = "Item " + i + ": ";
    attributes.add(new ViewAttribute<>(prefix + "Title", item.getTitle()));
    attributes.add(new ViewAttribute<>(prefix + "ContentDescription",
        MenuItemCompat.getContentDescription(item)));
    attributes.add(new ViewAttribute<>(prefix + "TooltipText", MenuItemCompat.getTooltipText(item)));
    attributes.add(new ViewAttribute<>(prefix + "Checkable", item.isCheckable()));
    attributes.add(new ViewAttribute<>(prefix + "Checked", item.isChecked()));
    attributes.add(new ViewAttribute<>(prefix + "Enabled", item.isEnabled()));
    attributes.add(new ViewAttribute<>(prefix + "Visible", item.isVisible()));
    attributes.add(new ViewAttribute<>(prefix + "Order", item.getOrder()));
    attributes.add(new ViewAttribute<Void>(prefix + "Icon", item.getIcon()));
    attributes.add(createColorAttribute("IconTint",
        MenuItemCompat.getIconTintList(item)));
    attributes.add(new ViewAttribute<>("IconTintMode",
        new PorterDuffModeValue(MenuItemCompat.getIconTintMode(item))));
    attributes.add(new ViewAttribute<>(prefix + "Id", new ResourceValue(res, item.getItemId())));
    attributes.add(new ViewAttribute<>(prefix + "GroupId", new ResourceValue(res, item.getGroupId())));
  }
  return attributes;
}

代码示例来源:origin: michael-rapp/ChromeLikeTabSwitcher

Condition.INSTANCE.ensureNotNull(menu, "The menu may not be null");
for (int i = 0; i < menu.size(); i++) {
  MenuItem menuItem = menu.getItem(i);
  View view = menuItem.getActionView();

代码示例来源:origin: fossasia/pslab-android

private void selectNavMenu() {
  int size_menu = navigationView.getMenu().size();
  for (int i = 0; i < size_menu; i++) {
    final MenuItem item = navigationView.getMenu().getItem(i);
    if (item.hasSubMenu()) {
      unCheckAllMenuItems(item.getSubMenu());
    } else {
      item.setChecked(false);
    }
  }
  switch (navItemIndex) {
    case 0:
    case 1:
    case 2:
      navigationView.getMenu().getItem(navItemIndex).setChecked(true);
      break;
    case 3:
      navigationView.getMenu().getItem(4).getSubMenu().getItem(1).setChecked(true);
      break;
    case 4:
      navigationView.getMenu().getItem(4).getSubMenu().getItem(0).setChecked(true);
      break;
    default:
      navigationView.getMenu().getItem(0).setChecked(true);
      break;
  }
}

代码示例来源:origin: rubensousa/FloatingToolbar

/**
 * Place the menu items with icons inside a horizontal LinearLayout
 */
private void addMenuItems() {
  if (mMenu == null) {
    mMenu = new MenuBuilder(getContext());
    new SupportMenuInflater(getContext()).inflate(mMenuRes, mMenu);
  }
  LinearLayoutCompat.LayoutParams layoutParams
      = new LinearLayoutCompat.LayoutParams(LayoutParams.WRAP_CONTENT,
      LayoutParams.MATCH_PARENT, 1);
  setWeightSum(mMenu.size());
  for (int i = 0; i < mMenu.size(); i++) {
    MenuItem item = mMenu.getItem(i);
    if (item.isVisible()) {
      AppCompatImageButton imageButton = new AppCompatImageButton(getContext());
      //noinspection ResourceType
      imageButton.setId(item.getItemId() == Menu.NONE ? genViewId() : item.getItemId());
      imageButton.setBackgroundResource(mItemBackground);
      imageButton.setImageDrawable(item.getIcon());
      imageButton.setOnClickListener(this);
      imageButton.setOnLongClickListener(this);
      imageButton.setTag(item);
      mMenuLayout.addView(imageButton, layoutParams);
    }
  }
}

相关文章