diff --git a/core/java/android/view/LayoutInflater.java b/core/java/android/view/LayoutInflater.java index aa43bad85a36a..ed758504855e7 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. + *
+ * 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. + *
+ * 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("