2727import android .view .MotionEvent ;
2828import android .view .View ;
2929import android .view .accessibility .AccessibilityEvent ;
30- import android .widget .Button ;
31- import android .widget .ImageButton ;
32- import android .widget .LinearLayout ;
30+ import android .widget .TextView ;
3331import android .widget .Toast ;
3432
3533/**
3634 * @hide
3735 */
38- public class ActionMenuItemView extends LinearLayout
36+ public class ActionMenuItemView extends TextView
3937 implements MenuView .ItemView , View .OnClickListener , View .OnLongClickListener ,
4038 ActionMenuView .ActionMenuChildView {
4139 private static final String TAG = "ActionMenuItemView" ;
4240
4341 private MenuItemImpl mItemData ;
4442 private CharSequence mTitle ;
43+ private Drawable mIcon ;
4544 private MenuBuilder .ItemInvoker mItemInvoker ;
4645
47- private ImageButton mImageButton ;
48- private Button mTextButton ;
4946 private boolean mAllowTextWithIcon ;
5047 private boolean mExpandedFormat ;
5148 private int mMinWidth ;
49+ private int mSavedPaddingLeft ;
5250
5351 public ActionMenuItemView (Context context ) {
5452 this (context , null );
@@ -68,17 +66,12 @@ public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
6866 mMinWidth = a .getDimensionPixelSize (
6967 com .android .internal .R .styleable .ActionMenuItemView_minWidth , 0 );
7068 a .recycle ();
71- }
7269
73- @ Override
74- public void onFinishInflate () {
75- mImageButton = (ImageButton ) findViewById (com .android .internal .R .id .imageButton );
76- mTextButton = (Button ) findViewById (com .android .internal .R .id .textButton );
77- mImageButton .setOnClickListener (this );
78- mTextButton .setOnClickListener (this );
79- mImageButton .setOnLongClickListener (this );
8070 setOnClickListener (this );
8171 setOnLongClickListener (this );
72+
73+ // Save the inflated padding for later, we'll need it.
74+ mSavedPaddingLeft = getPaddingLeft ();
8275 }
8376
8477 public MenuItemImpl getItemData () {
@@ -96,13 +89,6 @@ public void initialize(MenuItemImpl itemData, int menuType) {
9689 setEnabled (itemData .isEnabled ());
9790 }
9891
99- @ Override
100- public void setEnabled (boolean enabled ) {
101- super .setEnabled (enabled );
102- mImageButton .setEnabled (enabled );
103- mTextButton .setEnabled (enabled );
104- }
105-
10692 public void onClick (View v ) {
10793 if (mItemInvoker != null ) {
10894 mItemInvoker .invokeItem (mItemData );
@@ -135,26 +121,22 @@ public void setExpandedFormat(boolean expandedFormat) {
135121 }
136122
137123 private void updateTextButtonVisibility () {
138- boolean visible = !TextUtils .isEmpty (mTextButton . getText () );
139- visible &= mImageButton . getDrawable () == null ||
124+ boolean visible = !TextUtils .isEmpty (mTitle );
125+ visible &= mIcon == null ||
140126 (mItemData .showsTextAsAction () && (mAllowTextWithIcon || mExpandedFormat ));
141127
142- mTextButton . setVisibility (visible ? VISIBLE : GONE );
128+ setText (visible ? mTitle : null );
143129 }
144130
145131 public void setIcon (Drawable icon ) {
146- mImageButton .setImageDrawable (icon );
147- if (icon != null ) {
148- mImageButton .setVisibility (VISIBLE );
149- } else {
150- mImageButton .setVisibility (GONE );
151- }
132+ mIcon = icon ;
133+ setCompoundDrawablesWithIntrinsicBounds (icon , null , null , null );
152134
153135 updateTextButtonVisibility ();
154136 }
155137
156138 public boolean hasText () {
157- return mTextButton . getVisibility () != GONE ;
139+ return ! TextUtils . isEmpty ( getText ()) ;
158140 }
159141
160142 public void setShortcut (boolean showShortcut , char shortcutKey ) {
@@ -164,8 +146,6 @@ public void setShortcut(boolean showShortcut, char shortcutKey) {
164146 public void setTitle (CharSequence title ) {
165147 mTitle = title ;
166148
167- mTextButton .setText (mTitle );
168-
169149 setContentDescription (mTitle );
170150 updateTextButtonVisibility ();
171151 }
@@ -236,18 +216,31 @@ public boolean onLongClick(View v) {
236216
237217 @ Override
238218 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
219+ final boolean textVisible = hasText ();
220+ if (textVisible ) {
221+ setPadding (mSavedPaddingLeft , getPaddingTop (), getPaddingRight (), getPaddingBottom ());
222+ }
223+
239224 super .onMeasure (widthMeasureSpec , heightMeasureSpec );
240225
241226 final int widthMode = MeasureSpec .getMode (widthMeasureSpec );
242- final int specSize = MeasureSpec .getSize (widthMeasureSpec );
227+ final int widthSize = MeasureSpec .getSize (widthMeasureSpec );
243228 final int oldMeasuredWidth = getMeasuredWidth ();
244- final int targetWidth = widthMode == MeasureSpec .AT_MOST ? Math .min (specSize , mMinWidth )
229+ final int targetWidth = widthMode == MeasureSpec .AT_MOST ? Math .min (widthSize , mMinWidth )
245230 : mMinWidth ;
246231
247232 if (widthMode != MeasureSpec .EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth ) {
248233 // Remeasure at exactly the minimum width.
249234 super .onMeasure (MeasureSpec .makeMeasureSpec (targetWidth , MeasureSpec .EXACTLY ),
250235 heightMeasureSpec );
251236 }
237+
238+ if (!textVisible && mIcon != null ) {
239+ // TextView won't center compound drawables in both dimensions without
240+ // a little coercion. Pad in to center the icon after we've measured.
241+ final int w = getMeasuredWidth ();
242+ final int dw = mIcon .getIntrinsicWidth ();
243+ setPadding ((w - dw ) / 2 , getPaddingTop (), getPaddingRight (), getPaddingBottom ());
244+ }
252245 }
253246}
0 commit comments