LayoutInflater: support android:theme attribute

This commit is contained in:
Julian Winkler
2023-11-08 21:29:46 +01:00
parent d025fd3ce3
commit 9f74ab811e
3 changed files with 25 additions and 2 deletions

View File

@@ -1229,7 +1229,7 @@ public class Resources {
* @param other The existing Theme to copy from. * @param other The existing Theme to copy from.
*/ */
public void setTo(Theme other) { public void setTo(Theme other) {
themeMap = other.themeMap; themeMap = new HashMap<>(other.themeMap);
} }
/** /**

View File

@@ -2,11 +2,26 @@ package android.view;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.res.Resources;
public class ContextThemeWrapper extends ContextWrapper { public class ContextThemeWrapper extends ContextWrapper {
private Resources.Theme theme = getResources().newTheme();
public ContextThemeWrapper(Context context, int themeResId) { public ContextThemeWrapper(Context context, int themeResId) {
super(context); super(context);
theme.setTo(context.getTheme());
setTheme(themeResId);
}
@Override
public void setTheme(int resid) {
theme.applyStyle(resid, true);
}
@Override
public Resources.Theme getTheme() {
return theme;
} }
} }

View File

@@ -1,6 +1,7 @@
package android.view; package android.view;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Slog; import android.util.Slog;
@@ -42,7 +43,14 @@ public class LayoutInflater {
Constructor constructor = view_class.getConstructor(Context.class, AttributeSet.class); Constructor constructor = view_class.getConstructor(Context.class, AttributeSet.class);
View view_instance = (View)constructor.newInstance(Context.this_application, attrs); Context context = Context.this_application;
final TypedArray ta = context.obtainStyledAttributes(attrs, new int[]{com.android.internal.R.attr.theme});
final int themeResId = ta.getResourceId(0, 0);
if (themeResId != 0) {
context = new ContextThemeWrapper(context, themeResId);
}
ta.recycle();
View view_instance = (View)constructor.newInstance(context, attrs);
return view_instance; return view_instance;
} }