From eb97f473fc075c91760c2b657a7014681cd6f3ce Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Sat, 19 Jul 2014 11:57:21 -0700 Subject: [PATCH] Adapt ActionBar.LayoutParams to Toolbar.LayoutParams on demand When Toolbar is acting in the role of an action bar it needs to handle the case where app code calls setLayoutParams on a custom view with an ActionBar.LayoutParams instance. Run the newly set LayoutParams through the check/generate routine normally performed when adding a child view whenever LayoutParams are set on an existing child to correct for it. This might not be a bad thing to generalize someday but it would make for very confusing compatibility behavior when running code that was written to expect it on older platform versions. Bug 16219488 Change-Id: I568a335fe8ebbbaa666690d1f0e95f313abd2f1e --- core/java/android/widget/Toolbar.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/java/android/widget/Toolbar.java b/core/java/android/widget/Toolbar.java index 712e6d0370b29..c0d8764af22e9 100644 --- a/core/java/android/widget/Toolbar.java +++ b/core/java/android/widget/Toolbar.java @@ -1006,6 +1006,24 @@ public class Toolbar extends ViewGroup { super.onRestoreInstanceState(ss.getSuperState()); } + /** + * @hide + */ + @Override + protected void onSetLayoutParams(View child, ViewGroup.LayoutParams lp) { + /* + * Apps may set ActionBar.LayoutParams on their action bar custom views when + * a Toolbar is actually acting in the role of the action bar. Perform a quick + * switch with Toolbar.LayoutParams whenever this happens. This does leave open + * one potential gotcha: if an app retains the ActionBar.LayoutParams reference + * and attempts to keep making changes to it before layout those changes won't + * be reflected in the final results. + */ + if (!checkLayoutParams(lp)) { + child.setLayoutParams(generateLayoutParams(lp)); + } + } + private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed, int parentHeightSpec, int heightUsed, int heightConstraint) { final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();