diff options
author | Alan Viverette <alanv@google.com> | 2014-01-08 20:40:38 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2014-01-08 20:40:39 +0000 |
commit | 25838002c81dc57bd271ba026a2d4aa85e7eee90 (patch) | |
tree | 6edec567f19490b6d1ad46d7e99f2454d8be4c09 | |
parent | e72e85f42a569ccf229c3ca02d9af87f63e485f5 (diff) | |
parent | 24927f2798fdeee3aa4fa13dee5acfa218e993e3 (diff) | |
download | frameworks_base-25838002c81dc57bd271ba026a2d4aa85e7eee90.zip frameworks_base-25838002c81dc57bd271ba026a2d4aa85e7eee90.tar.gz frameworks_base-25838002c81dc57bd271ba026a2d4aa85e7eee90.tar.bz2 |
Merge "Allow Views to specify a theme override"
-rw-r--r-- | core/java/android/view/LayoutInflater.java | 103 |
1 files changed, 67 insertions, 36 deletions
diff --git a/core/java/android/view/LayoutInflater.java b/core/java/android/view/LayoutInflater.java index aa43bad..ed75850 100644 --- a/core/java/android/view/LayoutInflater.java +++ b/core/java/android/view/LayoutInflater.java @@ -91,6 +91,8 @@ public abstract class LayoutInflater { private static final String TAG_1995 = "blink"; private static final String TAG_REQUEST_FOCUS = "requestFocus"; + private static final String ATTR_THEME = "theme"; + /** * Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed * to be inflated. @@ -459,15 +461,10 @@ public abstract class LayoutInflater { + "ViewGroup root and attachToRoot=true"); } - rInflate(parser, root, attrs, false); + rInflate(parser, root, attrs, false, false); } else { // Temp is the root view that was found in the xml - View temp; - if (TAG_1995.equals(name)) { - temp = new BlinkLayout(mContext, attrs); - } else { - temp = createViewFromTag(root, name, attrs); - } + final View temp = createViewFromTag(root, name, attrs, false); ViewGroup.LayoutParams params = null; @@ -489,7 +486,7 @@ public abstract class LayoutInflater { System.out.println("-----> start inflating children"); } // Inflate all children under temp - rInflate(parser, temp, attrs, true); + rInflate(parser, temp, attrs, true, true); if (DEBUG) { System.out.println("-----> done inflating children"); } @@ -669,31 +666,66 @@ public abstract class LayoutInflater { return onCreateView(name, attrs); } - /* - * default visibility so the BridgeInflater can override it. + /** + * Creates a view from a tag name using the supplied attribute set. + * <p> + * If {@code inheritContext} is true and the parent is non-null, the view + * will be inflated in parent view's context. If the view specifies a + * <theme> attribute, the inflation context will be wrapped with the + * specified theme. + * <p> + * Note: Default visibility so the BridgeInflater can override it. */ - View createViewFromTag(View parent, String name, AttributeSet attrs) { + View createViewFromTag(View parent, String name, AttributeSet attrs, boolean inheritContext) { if (name.equals("view")) { name = attrs.getAttributeValue(null, "class"); } + Context viewContext; + if (parent != null && inheritContext) { + viewContext = parent.getContext(); + } else { + viewContext = mContext; + } + + // Apply a theme wrapper, if requested. + final int themeResId = attrs.getAttributeResourceValue(null, ATTR_THEME, 0); + if (themeResId != 0) { + viewContext = new ContextThemeWrapper(viewContext, themeResId); + } + + if (name.equals(TAG_1995)) { + // Let's party like it's 1995! + return new BlinkLayout(viewContext, attrs); + } + if (DEBUG) System.out.println("******** Creating view: " + name); try { View view; - if (mFactory2 != null) view = mFactory2.onCreateView(parent, name, mContext, attrs); - else if (mFactory != null) view = mFactory.onCreateView(name, mContext, attrs); - else view = null; + if (mFactory2 != null) { + view = mFactory2.onCreateView(parent, name, viewContext, attrs); + } else if (mFactory != null) { + view = mFactory.onCreateView(name, viewContext, attrs); + } else { + view = null; + } if (view == null && mPrivateFactory != null) { - view = mPrivateFactory.onCreateView(parent, name, mContext, attrs); + view = mPrivateFactory.onCreateView(parent, name, viewContext, attrs); } - + if (view == null) { - if (-1 == name.indexOf('.')) { - view = onCreateView(parent, name, attrs); - } else { - view = createView(name, null, attrs); + final Object lastContext = mConstructorArgs[0]; + mConstructorArgs[0] = viewContext; + try { + if (-1 == name.indexOf('.')) { + view = onCreateView(parent, name, attrs); + } else { + view = createView(name, null, attrs); + } + } finally { + mConstructorArgs[0] = lastContext; } } @@ -720,9 +752,14 @@ public abstract class LayoutInflater { /** * Recursive method used to descend down the xml hierarchy and instantiate * views, instantiate their children, and then call onFinishInflate(). + * + * @param inheritContext Whether the root view should be inflated in its + * parent's context. This should be true when called inflating + * child views recursively, or false otherwise. */ void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs, - boolean finishInflate) throws XmlPullParserException, IOException { + boolean finishInflate, boolean inheritContext) throws XmlPullParserException, + IOException { final int depth = parser.getDepth(); int type; @@ -742,20 +779,14 @@ public abstract class LayoutInflater { if (parser.getDepth() == 0) { throw new InflateException("<include /> cannot be the root element"); } - parseInclude(parser, parent, attrs); + parseInclude(parser, parent, attrs, inheritContext); } else if (TAG_MERGE.equals(name)) { throw new InflateException("<merge /> must be the root element"); - } else if (TAG_1995.equals(name)) { - final View view = new BlinkLayout(mContext, attrs); - final ViewGroup viewGroup = (ViewGroup) parent; - final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); - rInflate(parser, view, attrs, true); - viewGroup.addView(view, params); } else { - final View view = createViewFromTag(parent, name, attrs); + final View view = createViewFromTag(parent, name, attrs, inheritContext); final ViewGroup viewGroup = (ViewGroup) parent; final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); - rInflate(parser, view, attrs, true); + rInflate(parser, view, attrs, true, true); viewGroup.addView(view, params); } } @@ -774,9 +805,8 @@ public abstract class LayoutInflater { } } - private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs) - throws XmlPullParserException, IOException { - + private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs, + boolean inheritContext) throws XmlPullParserException, IOException { int type; if (parent instanceof ViewGroup) { @@ -811,9 +841,10 @@ public abstract class LayoutInflater { if (TAG_MERGE.equals(childName)) { // Inflate all children. - rInflate(childParser, parent, childAttrs, false); + rInflate(childParser, parent, childAttrs, false, inheritContext); } else { - final View view = createViewFromTag(parent, childName, childAttrs); + final View view = createViewFromTag(parent, childName, childAttrs, + inheritContext); final ViewGroup group = (ViewGroup) parent; // We try to load the layout params set in the <include /> tag. If @@ -836,7 +867,7 @@ public abstract class LayoutInflater { } // Inflate all children. - rInflate(childParser, view, childAttrs, true); + rInflate(childParser, view, childAttrs, true, true); // Attempt to override the included layout's android:id with the // one set on the <include /> tag itself. |