From 9f74ab811eee1d688a12b0a9a529b2b9fcef7de0 Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Wed, 8 Nov 2023 21:29:46 +0100 Subject: [PATCH] LayoutInflater: support android:theme attribute --- src/api-impl/android/content/res/Resources.java | 2 +- .../android/view/ContextThemeWrapper.java | 15 +++++++++++++++ src/api-impl/android/view/LayoutInflater.java | 10 +++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/api-impl/android/content/res/Resources.java b/src/api-impl/android/content/res/Resources.java index a8f5e28f..5938c109 100644 --- a/src/api-impl/android/content/res/Resources.java +++ b/src/api-impl/android/content/res/Resources.java @@ -1229,7 +1229,7 @@ public class Resources { * @param other The existing Theme to copy from. */ public void setTo(Theme other) { - themeMap = other.themeMap; + themeMap = new HashMap<>(other.themeMap); } /** diff --git a/src/api-impl/android/view/ContextThemeWrapper.java b/src/api-impl/android/view/ContextThemeWrapper.java index 06ef6de4..63e51a2a 100644 --- a/src/api-impl/android/view/ContextThemeWrapper.java +++ b/src/api-impl/android/view/ContextThemeWrapper.java @@ -2,11 +2,26 @@ package android.view; import android.content.Context; import android.content.ContextWrapper; +import android.content.res.Resources; public class ContextThemeWrapper extends ContextWrapper { + private Resources.Theme theme = getResources().newTheme(); + public ContextThemeWrapper(Context context, int themeResId) { 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; } } diff --git a/src/api-impl/android/view/LayoutInflater.java b/src/api-impl/android/view/LayoutInflater.java index 93baea45..300e17ec 100644 --- a/src/api-impl/android/view/LayoutInflater.java +++ b/src/api-impl/android/view/LayoutInflater.java @@ -1,6 +1,7 @@ package android.view; import android.content.Context; +import android.content.res.TypedArray; import android.content.res.XmlResourceParser; import android.util.AttributeSet; import android.util.Slog; @@ -42,7 +43,14 @@ public class LayoutInflater { 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; }