下面列出了android.view.MenuItem#getTitle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void trackMenuItem(Object object, MenuItem menuItem, boolean hasTrackClickAnn,long currentTime) {
try {
if (isTrackClickSwitchClose()) {
return;
}
Object pageObj = null;
if (!AllegroUtils.isPage(object)) {
pageObj = AllegroUtils.getPageObjFromView(null);
}
if (!checkTrackClickEnable(pageObj, menuItem, hasTrackClickAnn)) {
return;
}
Map<String, Object> elementInfo = new HashMap<>();
elementInfo.put(Constants.ELEMENT_TYPE, "MenuItem");
CharSequence title = menuItem.getTitle();
if (!TextUtils.isEmpty(title)) {
elementInfo.put(Constants.ELEMENT_CONTENT, title);
}
String idName = AllegroUtils.getIdResourceName(menuItem.getItemId());
if (!TextUtils.isEmpty(idName)) {
elementInfo.put(Constants.ELEMENT_ID, idName);
}
autoTrackClick(pageObj, elementInfo, hasTrackClickAnn,currentTime);
} catch (Throwable ignore) {
ExceptionUtil.exceptionThrow(ignore);
}
}
private void markPro(MenuItem menu, String sku) {
if (sku == null || !IAB.isPurchased(sku, this)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean dark = prefs.getBoolean("dark_theme", false);
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + menu.getTitle());
ssb.setSpan(new ImageSpan(this, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
menu.setTitle(ssb);
}
}
private void markPro(Context context, MenuItem menu, String sku) {
if (sku == null || !IAB.isPurchased(sku, context)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dark = prefs.getBoolean("dark_theme", false);
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + menu.getTitle());
ssb.setSpan(new ImageSpan(context, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
menu.setTitle(ssb);
}
}
private void markPro(Context context, MenuItem menu, String sku) {
if (sku == null || !IAB.isPurchased(sku, context)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dark = prefs.getBoolean("dark_theme", false);
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + menu.getTitle());
ssb.setSpan(new ImageSpan(context, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
menu.setTitle(ssb);
}
}
private View createFabMenuItem(MenuItem menuItem) {
ViewGroup fabMenuItem = (ViewGroup) LayoutInflater.from(getContext())
.inflate(getMenuItemLayoutId(), this, false);
FloatingActionButton miniFab = (FloatingActionButton) fabMenuItem.findViewById(R.id.mini_fab);
FrameLayout cardView = (FrameLayout) fabMenuItem.findViewById(R.id.card_view);
TextView titleView = (TextView) fabMenuItem.findViewById(R.id.title_view);
fabMenuItemMap.put(miniFab, menuItem);
cardViewMenuItemMap.put(cardView, menuItem);
miniFab.setImageDrawable(menuItem.getIcon());
miniFab.setOnClickListener(this);
cardView.setOnClickListener(this);
ViewCompat.setAlpha(miniFab, 0f);
ViewCompat.setAlpha(cardView, 0f);
final CharSequence title = menuItem.getTitle();
if (!TextUtils.isEmpty(title) && miniFabTitlesEnabled) {
//cardView.setCardBackgroundColor(miniFabTitleBackgroundTint.getDefaultColor());
titleView.setText(title);
titleView.setTypeface(null, Typeface.BOLD);
titleView.setTextColor(miniFabTitleTextColor);
} else {
fabMenuItem.removeView(cardView);
}
miniFab.setBackgroundTintList(miniFabBackgroundTint);
if (Utils.hasLollipop()) {
miniFab.setImageTintList(miniFabDrawableTint);
}
return fabMenuItem;
}
public Item(MenuItem item) {
id = item.getItemId();
icon = item.getIcon();
tint = MenuItemCompat.getIconTintList(item);
groupId = item.getGroupId();
title = item.getTitle();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (theme.isBaseLight()) {
int black = ContextCompat.getColor(this, R.color.black);
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new ForegroundColorSpan(black), 0, s.length(), 0);
item.setTitle(s);
}
}
return super.onPrepareOptionsMenu(menu);
}
private CharSequence getTitleToRestore(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.navigation_item_experiments) {
return getResources().getString(R.string.app_name);
} else {
return menuItem.getTitle();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle() != null
&& item.getTitle().equals(getActivity().getString(R.string.action_reply))) {
onParentReply();
return true;
}
return false;
}
@SuppressWarnings("RestrictTo")
public void showChooseThemePopup(View anchor) {
PopupMenu popupMenu = new PopupMenu(context, anchor);
popupMenu.inflate(R.menu.mtrl_choose_theme_menu);
if (popupMenu.getMenu() instanceof MenuBuilder) {
MenuBuilder menuBuilder = (MenuBuilder) popupMenu.getMenu();
menuBuilder.setOptionalIconsVisible(true);
ColorStateList defaultColor =
AppCompatResources.getColorStateList(
context, R.color.material_on_surface_emphasis_medium);
int selectedColor = MaterialColors.getColor(anchor, resourceProvider.getPrimaryColor());
int currentThemeId = getCurrentThemeId();
for (int i = 0; i < menuBuilder.size(); i++) {
MenuItem item = menuBuilder.getItem(i);
if (item.getItemId() == currentThemeId) {
DrawableCompat.setTint(item.getIcon(), selectedColor);
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new ForegroundColorSpan(selectedColor), 0, s.length(), 0);
item.setTitle(s);
} else {
DrawableCompat.setTintList(item.getIcon(), defaultColor);
}
}
}
popupMenu.setOnMenuItemClickListener(
item -> {
saveAndApplyTheme(item.getItemId());
return false;
});
popupMenu.show();
}
public Item(MenuItem menuItem) {
id = menuItem.getItemId();
try { // breaks preview
this.icon = menuItem.getIcon();
} catch (Exception e) {
}
this.text = menuItem.getTitle();
iconTint = MenuItemCompat.getIconTintList(menuItem);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle() == null){
finish();
return true;
}
return false;
}
private void applyFontToMenuItem(MenuItem mi) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new RobotoLightTypefaceSpan("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
private View createFabMenuItem(MenuItem menuItem) {
ViewGroup fabMenuItem = (ViewGroup) LayoutInflater.from(getContext())
.inflate(getMenuItemLayoutId(), this, false);
FloatingActionButton miniFab = (FloatingActionButton) fabMenuItem.findViewById(R.id.mini_fab);
CardView cardView = (CardView) fabMenuItem.findViewById(R.id.card_view);
TextView titleView = (TextView) fabMenuItem.findViewById(R.id.title_view);
fabMenuItemMap.put(miniFab, menuItem);
cardViewMenuItemMap.put(cardView, menuItem);
miniFab.setImageDrawable(menuItem.getIcon());
miniFab.setOnClickListener(this);
cardView.setOnClickListener(this);
ViewCompat.setAlpha(miniFab, 0f);
ViewCompat.setAlpha(cardView, 0f);
final CharSequence title = menuItem.getTitle();
if (!TextUtils.isEmpty(title) && miniFabTitlesEnabled) {
cardView.setCardBackgroundColor(miniFabTitleBackgroundTint.getDefaultColor());
titleView.setText(title);
titleView.setTypeface(null, Typeface.BOLD);
titleView.setTextColor(miniFabTitleTextColor);
if (miniFabTitleTextColorArray != null) {
titleView.setTextColor(ContextCompat.getColorStateList(getContext(),
miniFabTitleTextColorArray[menuItem.getOrder()]));
}
} else {
fabMenuItem.removeView(cardView);
}
miniFab.setBackgroundTintList(miniFabBackgroundTint);
if (miniFabBackgroundTintArray != null) {
miniFab.setBackgroundTintList(ContextCompat.getColorStateList(getContext(),
miniFabBackgroundTintArray[menuItem.getOrder()]));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
miniFab.setImageTintList(miniFabDrawableTint);
}
return fabMenuItem;
}
private void applyFontToMenuItem(MenuItem mi) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/SourceSansPro-Semibold.otf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
public Item(MenuItem item) {
icon = item.getIcon();
tint = MenuItemCompat.getIconTintList(item);
enabled = item.isEnabled();
title = item.getTitle();
}
public static void setMenuItemTextColor(MenuItem menuItem, @ColorInt int textColor) {
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(textColor), 0, s.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
menuItem.setTitle(s);
}
private List<BottomSheetItem> createAdapterItems(int dividerBackground, int titleTextColor,
int itemTextColor, int itemBackground,
int tintColor) {
List<BottomSheetItem> items = new ArrayList<>();
mTitles = 0;
boolean addedSubMenu = false;
for (int i = 0; i < mMenu.size(); i++) {
MenuItem item = mMenu.getItem(i);
if (item.isVisible()) {
if (item.hasSubMenu()) {
SubMenu subMenu = item.getSubMenu();
if (i != 0 && addedSubMenu) {
if (mMode == BottomSheetBuilder.MODE_GRID) {
throw new IllegalArgumentException("MODE_GRID can't have submenus." +
" Use MODE_LIST instead");
}
items.add(new BottomSheetDivider(dividerBackground));
}
CharSequence title = item.getTitle();
if (title != null && !title.equals("")) {
items.add(new BottomSheetHeader(title.toString(), titleTextColor));
mTitles++;
}
for (int j = 0; j < subMenu.size(); j++) {
MenuItem subItem = subMenu.getItem(j);
if (subItem.isVisible()) {
items.add(new BottomSheetMenuItem(subItem, itemTextColor,
itemBackground, tintColor));
addedSubMenu = true;
}
}
} else {
items.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
}
}
}
return items;
}
private void applyFontToMenuItem(MenuItem mi) {
Typeface fontType = FontManager.getTypeface(getApplicationContext(), FontManager.APP_FONT);
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypeFaceSpan("", fontType), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
private void applyFontToMenuItem(MenuItem mi) {
Typeface fontType = FontManager.getTypeface(getApplicationContext(), FontManager.APP_FONT);
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypeFaceSpan("", fontType), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}