summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Alan Viverette <alanv@google.com> 2016-02-16 19:05:59 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2016-02-16 19:06:00 +0000
commitf8872b0a37024c78dbfcf6f19d63c9555eec4709 (patch)
treefefc8dfc0607820f25ef28e3309194cd52a18b17
parent5ef5e0faf07469d716175896428b4e796fdbfed9 (diff)
parent60d9695bb84a5c2a8e9ef2df32b9d4105aea2b0a (diff)
Merge "Force uniform width for MATCH_PARENT in AlertDialogLayout" into nyc-dev
-rw-r--r--core/java/com/android/internal/widget/AlertDialogLayout.java45
1 files changed, 37 insertions, 8 deletions
diff --git a/core/java/com/android/internal/widget/AlertDialogLayout.java b/core/java/com/android/internal/widget/AlertDialogLayout.java
index cc0db51fb4d5..891c920af336 100644
--- a/core/java/com/android/internal/widget/AlertDialogLayout.java
+++ b/core/java/com/android/internal/widget/AlertDialogLayout.java
@@ -17,7 +17,6 @@
package com.android.internal.widget;
import android.annotation.AttrRes;
-import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.content.Context;
@@ -107,14 +106,7 @@ public class AlertDialogLayout extends LinearLayout {
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
-
- // Treat all panel widths as MATCH_PARENT
- // by translating AT_MOST to EXACTLY.
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
- if (widthMode == MeasureSpec.AT_MOST) {
- final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
- widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
- }
int childState = 0;
int usedHeight = getPaddingTop() + getPaddingBottom();
@@ -204,15 +196,52 @@ public class AlertDialogLayout extends LinearLayout {
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
}
}
+
maxWidth += getPaddingLeft() + getPaddingRight();
final int widthSizeAndState = resolveSizeAndState(maxWidth, widthMeasureSpec, childState);
final int heightSizeAndState = resolveSizeAndState(usedHeight, heightMeasureSpec, 0);
setMeasuredDimension(widthSizeAndState, heightSizeAndState);
+
+ // If the children weren't already measured EXACTLY, we need to run
+ // another measure pass to for MATCH_PARENT widths.
+ if (widthMode != MeasureSpec.EXACTLY) {
+ forceUniformWidth(count, heightMeasureSpec);
+ }
+
return true;
}
/**
+ * Remeasures child views to exactly match the layout's measured width.
+ *
+ * @param count the number of child views
+ * @param heightMeasureSpec the original height measure spec
+ */
+ private void forceUniformWidth(int count, int heightMeasureSpec) {
+ // Pretend that the linear layout has an exact size.
+ final int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(
+ getMeasuredWidth(), MeasureSpec.EXACTLY);
+
+ for (int i = 0; i < count; i++) {
+ final View child = getChildAt(i);
+ if (child.getVisibility() != GONE) {
+ final LayoutParams lp = (LayoutParams) child.getLayoutParams();
+ if (lp.width == LayoutParams.MATCH_PARENT) {
+ // Temporarily force children to reuse their old measured
+ // height.
+ final int oldHeight = lp.height;
+ lp.height = child.getMeasuredHeight();
+
+ // Remeasure with new dimensions.
+ measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0);
+ lp.height = oldHeight;
+ }
+ }
+ }
+ }
+
+ /**
* Attempts to resolve the minimum height of a view.
* <p>
* If the view doesn't have a minimum height set and only contains a single