Allow Views to specify a theme override
During inflation, any view with a theme attribute will wrap its context with a theme override. Child views inflated during recursion will inherit the wrapped context. BUG: 12178430 Change-Id: Icba019d1c4f4f7e1a66d19dd402038defb42b7a6
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user