diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Android/ResourcesProvider.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/ResourcesProvider.cs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/ResourcesProvider.cs b/Xamarin.Forms.Platform.Android/ResourcesProvider.cs new file mode 100644 index 00000000..334ef56a --- /dev/null +++ b/Xamarin.Forms.Platform.Android/ResourcesProvider.cs @@ -0,0 +1,68 @@ +using Android.Content; +using Android.Content.Res; +using Android.Util; + +namespace Xamarin.Forms.Platform.Android +{ + internal class ResourcesProvider : ISystemResourcesProvider + { + ResourceDictionary _dictionary; + + public IResourceDictionary GetSystemResources() + { + _dictionary = new ResourceDictionary(); + + UpdateStyles(); + + return _dictionary; + } + + public Style GetStyle(int style) + { + var result = new Style(typeof(Label)); + + double fontSize = 0; + string fontFamily = null; + global::Android.Graphics.Color defaultColor = global::Android.Graphics.Color.Argb(0, 0, 0, 0); + global::Android.Graphics.Color androidColor = defaultColor; + + Context context = Forms.Context; + using(var value = new TypedValue()) + { + if (context.Theme.ResolveAttribute(style, value, true)) + { + var styleattrs = new[] { global::Android.Resource.Attribute.TextSize, global::Android.Resource.Attribute.FontFamily, global::Android.Resource.Attribute.TextColor }; + using(TypedArray array = context.ObtainStyledAttributes(value.ResourceId, styleattrs)) + { + fontSize = context.FromPixels(array.GetDimensionPixelSize(0, -1)); + fontFamily = array.GetString(1); + androidColor = array.GetColor(2, defaultColor); + } + } + } + + if (fontSize > 0) + result.Setters.Add(new Setter { Property = Label.FontSizeProperty, Value = fontSize }); + + if (!string.IsNullOrEmpty(fontFamily)) + result.Setters.Add(new Setter { Property = Label.FontFamilyProperty, Value = fontFamily }); + + if (androidColor != defaultColor) + { + result.Setters.Add(new Setter { Property = Label.TextColorProperty, Value = Color.FromRgba(androidColor.R, androidColor.G, androidColor.B, androidColor.A) }); + } + + return result; + } + + void UpdateStyles() + { + _dictionary[Device.Styles.BodyStyleKey] = new Style(typeof(Label)); // do nothing, its fine + _dictionary[Device.Styles.TitleStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceLarge); + _dictionary[Device.Styles.SubtitleStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceMedium); + _dictionary[Device.Styles.CaptionStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceSmall); + _dictionary[Device.Styles.ListItemTextStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceListItem); + _dictionary[Device.Styles.ListItemDetailTextStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceListItemSmall); + } + } +}
\ No newline at end of file |