IconMenuItemViewpublic final class IconMenuItemView extends android.widget.TextView implements MenuView.ItemViewThe item view for each item in the {@link IconMenuView}. |
Fields Summary |
---|
private static final int | NO_ALPHA | private IconMenuView | mIconMenuView | private com.android.internal.view.menu.MenuBuilder.ItemInvoker | mItemInvoker | private MenuItemImpl | mItemData | private android.graphics.drawable.Drawable | mIcon | private int | mTextAppearance | private android.content.Context | mTextAppearanceContext | private float | mDisabledAlpha | private android.graphics.Rect | mPositionIconAvailable | private android.graphics.Rect | mPositionIconOutput | private boolean | mShortcutCaptionMode | private String | mShortcutCaption | private static String | sPrependShortcutLabel |
Constructors Summary |
---|
public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
if (sPrependShortcutLabel == null) {
/*
* Views should only be constructed from the UI thread, so no
* synchronization needed
*/
sPrependShortcutLabel = getResources().getString(
com.android.internal.R.string.prepend_shortcut_label);
}
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.MenuView, defStyleAttr, defStyleRes);
mDisabledAlpha = a.getFloat(
com.android.internal.R.styleable.MenuView_itemIconDisabledAlpha, 0.8f);
mTextAppearance = a.getResourceId(com.android.internal.R.styleable.
MenuView_itemTextAppearance, -1);
mTextAppearanceContext = context;
a.recycle();
| public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)
this(context, attrs, defStyleAttr, 0);
| public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs)
this(context, attrs, 0);
|
Methods Summary |
---|
protected void | drawableStateChanged()
super.drawableStateChanged();
if (mItemData != null && mIcon != null) {
// When disabled, the not-focused state and the pressed state should
// drop alpha on the icon
final boolean isInAlphaState = !mItemData.isEnabled() && (isPressed() || !isFocused());
mIcon.setAlpha(isInAlphaState ? (int) (mDisabledAlpha * NO_ALPHA) : NO_ALPHA);
}
| public MenuItemImpl | getItemData()
return mItemData;
| IconMenuView.LayoutParams | getTextAppropriateLayoutParams()
IconMenuView.LayoutParams lp = (IconMenuView.LayoutParams) getLayoutParams();
if (lp == null) {
// Default layout parameters
lp = new IconMenuView.LayoutParams(
IconMenuView.LayoutParams.MATCH_PARENT, IconMenuView.LayoutParams.MATCH_PARENT);
}
// Set the desired width of item
lp.desiredWidth = (int) Layout.getDesiredWidth(getText(), getPaint());
return lp;
| void | initialize(java.lang.CharSequence title, android.graphics.drawable.Drawable icon)Initializes with the provided title and icon
setClickable(true);
setFocusable(true);
if (mTextAppearance != -1) {
setTextAppearance(mTextAppearanceContext, mTextAppearance);
}
setTitle(title);
setIcon(icon);
| public void | initialize(MenuItemImpl itemData, int menuType)
mItemData = itemData;
initialize(itemData.getTitleForItemView(this), itemData.getIcon());
setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
setEnabled(itemData.isEnabled());
| protected void | onLayout(boolean changed, int left, int top, int right, int bottom)
super.onLayout(changed, left, top, right, bottom);
positionIcon();
| protected void | onTextChanged(java.lang.CharSequence text, int start, int before, int after)
super.onTextChanged(text, start, before, after);
// our layout params depend on the length of the text
setLayoutParams(getTextAppropriateLayoutParams());
| public boolean | performClick()
// Let the view's click listener have top priority (the More button relies on this)
if (super.performClick()) {
return true;
}
if ((mItemInvoker != null) && (mItemInvoker.invokeItem(mItemData))) {
playSoundEffect(SoundEffectConstants.CLICK);
return true;
} else {
return false;
}
| private void | positionIcon()Positions the icon vertically (horizontal centering is taken care of by
the TextView's gravity).
if (mIcon == null) {
return;
}
// We reuse the output rectangle as a temp rect
Rect tmpRect = mPositionIconOutput;
getLineBounds(0, tmpRect);
mPositionIconAvailable.set(0, 0, getWidth(), tmpRect.top);
final int layoutDirection = getLayoutDirection();
Gravity.apply(Gravity.CENTER_VERTICAL | Gravity.START, mIcon.getIntrinsicWidth(), mIcon
.getIntrinsicHeight(), mPositionIconAvailable, mPositionIconOutput,
layoutDirection);
mIcon.setBounds(mPositionIconOutput);
| public boolean | prefersCondensedTitle()
return true;
| void | setCaptionMode(boolean shortcut)
/*
* If there is no item model, don't do any of the below (for example,
* the 'More' item doesn't have a model)
*/
if (mItemData == null) {
return;
}
mShortcutCaptionMode = shortcut && (mItemData.shouldShowShortcut());
CharSequence text = mItemData.getTitleForItemView(this);
if (mShortcutCaptionMode) {
if (mShortcutCaption == null) {
mShortcutCaption = mItemData.getShortcutLabel();
}
text = mShortcutCaption;
}
setText(text);
| public void | setCheckable(boolean checkable)
| public void | setChecked(boolean checked)
| public void | setIcon(android.graphics.drawable.Drawable icon)
mIcon = icon;
if (icon != null) {
/* Set the bounds of the icon since setCompoundDrawables needs it. */
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
// Set the compound drawables
setCompoundDrawables(null, icon, null, null);
// When there is an icon, make sure the text is at the bottom
setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
/*
* Request a layout to reposition the icon. The positioning of icon
* depends on this TextView's line bounds, which is only available
* after a layout.
*/
requestLayout();
} else {
setCompoundDrawables(null, null, null, null);
// When there is no icon, make sure the text is centered vertically
setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
| void | setIconMenuView(IconMenuView iconMenuView)
mIconMenuView = iconMenuView;
| public void | setItemData(MenuItemImpl data)
mItemData = data;
| public void | setItemInvoker(com.android.internal.view.menu.MenuBuilder.ItemInvoker itemInvoker)
mItemInvoker = itemInvoker;
| public void | setShortcut(boolean showShortcut, char shortcutKey)
if (mShortcutCaptionMode) {
/*
* Shortcut has changed and we're showing it right now, need to
* update (clear the old one first).
*/
mShortcutCaption = null;
setCaptionMode(true);
}
| public void | setTitle(java.lang.CharSequence title)
if (mShortcutCaptionMode) {
/*
* Don't set the title directly since it will replace the
* shortcut+title being shown. Instead, re-set the shortcut caption
* mode so the new title is shown.
*/
setCaptionMode(true);
} else if (title != null) {
setText(title);
}
| public void | setVisibility(int v)
super.setVisibility(v);
if (mIconMenuView != null) {
// On visibility change, mark the IconMenuView to refresh itself eventually
mIconMenuView.markStaleChildren();
}
| public boolean | showsIcon()
return true;
|
|