示例#1
1
        public Drawable GetIcon(Context context, Dictionary<string, int> overrideIcons = null)
        {
            if (overrideIcons == null)
                overrideIcons = new Dictionary<string, int> ();

            Drawable icon = null;

            var metrics = new List<DisplayMetricsDensity> {
                DisplayMetricsDensity.Xxxhigh,
                DisplayMetricsDensity.Xxhigh,
                DisplayMetricsDensity.Xhigh,
                DisplayMetricsDensity.Tv
            };

            if (App != null) {

                var pkgContext = context.CreatePackageContext (App.PackageName, PackageContextFlags.IgnoreSecurity);

                // Try and find the override first
                if (overrideIcons.ContainsKey (App.PackageName)) {
                    icon = context.Resources.GetDrawable (overrideIcons [App.PackageName]);
                } else {
                    // otherwise, go through the densities one by one
                    foreach (var m in metrics) {
                        try {
                            icon = pkgContext.Resources.GetDrawableForDensity (App.Icon, (int)m);
                            break;
                        } catch {
                            continue;
                        }
                    }
                }

            } else {
                if (overrideIcons.ContainsKey (PackageName))
                    icon = context.Resources.GetDrawable (overrideIcons [PackageName]);
            }

            return icon;
        }
		public static int GetThemeColor(Context context, int attribute, int defaultColor) {
			var themeColor = 0;
			var packageName = context.PackageName;
			try {
				var packageContext = context.CreatePackageContext(packageName, 0);
				var applicationInfo =
					context.PackageManager.GetApplicationInfo(packageName, 0);
				packageContext.SetTheme(applicationInfo.Theme);
				var theme = packageContext.Theme;
				var ta = theme.ObtainStyledAttributes(new [] {attribute});
				themeColor = ta.GetColor(0, defaultColor);
				ta.Recycle();
			} catch (PackageManager.NameNotFoundException e) {
				e.PrintStackTrace();
			}
			return themeColor;
		}