Skip to content

Commit 367ee32

Browse files
committed
Limit item heights in split action bars
Bug 6449422 Overzealous action views should not be able to cause action bars to take up a huge amount of space. Fixed text alignment on action buttons Change-Id: Ic28d6dacdb6933c63b323f5ed6a6cab5d3726938
1 parent b2269d6 commit 367ee32

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

core/java/com/android/internal/view/menu/ActionMenuItemView.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,13 @@ public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
7070
setOnClickListener(this);
7171
setOnLongClickListener(this);
7272

73-
// Save the inflated padding for later, we'll need it.
74-
mSavedPaddingLeft = getPaddingLeft();
73+
mSavedPaddingLeft = -1;
74+
}
75+
76+
@Override
77+
public void setPadding(int l, int t, int r, int b) {
78+
mSavedPaddingLeft = l;
79+
super.setPadding(l, t, r, b);
7580
}
7681

7782
public MenuItemImpl getItemData() {
@@ -217,8 +222,9 @@ public boolean onLongClick(View v) {
217222
@Override
218223
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
219224
final boolean textVisible = hasText();
220-
if (textVisible) {
221-
setPadding(mSavedPaddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
225+
if (textVisible && mSavedPaddingLeft >= 0) {
226+
super.setPadding(mSavedPaddingLeft, getPaddingTop(),
227+
getPaddingRight(), getPaddingBottom());
222228
}
223229

224230
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -240,7 +246,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
240246
// a little coercion. Pad in to center the icon after we've measured.
241247
final int w = getMeasuredWidth();
242248
final int dw = mIcon.getIntrinsicWidth();
243-
setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
249+
super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
244250
}
245251
}
246252
}

core/java/com/android/internal/view/menu/ActionMenuView.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.content.Context;
1919
import android.content.res.Configuration;
20+
import android.content.res.TypedArray;
2021
import android.util.AttributeSet;
2122
import android.view.Gravity;
2223
import android.view.View;
@@ -25,6 +26,8 @@
2526
import android.view.accessibility.AccessibilityEvent;
2627
import android.widget.LinearLayout;
2728

29+
import com.android.internal.R;
30+
2831
/**
2932
* @hide
3033
*/
@@ -43,6 +46,7 @@ public class ActionMenuView extends LinearLayout implements MenuBuilder.ItemInvo
4346
private int mMinCellSize;
4447
private int mGeneratedItemPadding;
4548
private int mMeasuredExtraWidth;
49+
private int mMaxItemHeight;
4650

4751
public ActionMenuView(Context context) {
4852
this(context, null);
@@ -54,6 +58,11 @@ public ActionMenuView(Context context, AttributeSet attrs) {
5458
final float density = context.getResources().getDisplayMetrics().density;
5559
mMinCellSize = (int) (MIN_CELL_SIZE * density);
5660
mGeneratedItemPadding = (int) (GENERATED_ITEM_PADDING * density);
61+
62+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar,
63+
R.attr.actionBarStyle, 0);
64+
mMaxItemHeight = a.getDimensionPixelSize(R.styleable.ActionBar_height, 0);
65+
a.recycle();
5766
}
5867

5968
public void setPresenter(ActionMenuPresenter presenter) {
@@ -116,6 +125,11 @@ private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) {
116125
final int widthPadding = getPaddingLeft() + getPaddingRight();
117126
final int heightPadding = getPaddingTop() + getPaddingBottom();
118127

128+
final int itemHeightSpec = heightMode == MeasureSpec.EXACTLY
129+
? MeasureSpec.makeMeasureSpec(heightSize - heightPadding, MeasureSpec.EXACTLY)
130+
: MeasureSpec.makeMeasureSpec(
131+
Math.min(mMaxItemHeight, heightSize - heightPadding), MeasureSpec.AT_MOST);
132+
119133
widthSize -= widthPadding;
120134

121135
// Divide the view into cells.
@@ -167,7 +181,7 @@ private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) {
167181
final int cellsAvailable = lp.isOverflowButton ? 1 : cellsRemaining;
168182

169183
final int cellsUsed = measureChildForCells(child, cellSize, cellsAvailable,
170-
heightMeasureSpec, heightPadding);
184+
itemHeightSpec, heightPadding);
171185

172186
maxCellsUsed = Math.max(maxCellsUsed, cellsUsed);
173187
if (lp.expandable) expandableItemCount++;
@@ -298,15 +312,15 @@ private void onMeasureExactFormat(int widthMeasureSpec, int heightMeasureSpec) {
298312

299313
// Remeasure any items that have had extra space allocated to them.
300314
if (needsExpansion) {
301-
int heightSpec = MeasureSpec.makeMeasureSpec(heightSize - heightPadding, heightMode);
302315
for (int i = 0; i < childCount; i++) {
303316
final View child = getChildAt(i);
304317
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
305318

306319
if (!lp.expanded) continue;
307320

308321
final int width = lp.cellsUsed * cellSize + lp.extraPixels;
309-
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), heightSpec);
322+
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
323+
itemHeightSpec);
310324
}
311325
}
312326

0 commit comments

Comments
 (0)