Android应用程序:动态地应用色彩主题在运行时 [英] Android App : Apply Color Theme Dynamically at Runtime

查看:232
本文介绍了Android应用程序:动态地应用色彩主题在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序要支持这样的功能,用户可以从列表中选择(这是从WebService的下载)色和用户选择任何颜色(比如橙色)在我的应用程序的所有按键都会有背景色为橙色。

In My Application I want to support such functionality where users can select color from the list (which is downloaded from WebService) and as User selects any color(say Orange) all the Buttons in my application will have background color as Orange.

我已经检查解决方案,如 HTTP:// WWW .androidengineer.com / 2010/06 /使用主题功能于Android的applications.html 但我需要在我的应用程序有不同的颜色产生不同的风格。

I have checked solutions like http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html but with that I need to have different styles created with different color in my app.

我需要的是我只支持一种风格只背景颜色需要根据选择改变,对于我不想在运行时去取我的所有按钮和应用的颜色。

My need is I only support one style only background colors need to be changed according selection and for that I don't want to fetch all my buttons at runtime and apply that color.

所以,请提出任何其他更好的解决方案,所选颜色应用到应用程序中所有的按钮并不获取所有按钮的活动,并设置其背景颜色。

So please suggest any other better solutions to apply selected color to all the buttons in the application without fetching all buttons in activities and setting their background color.

推荐答案

您可以填补一个位图与特定的颜色。

You could fill a Bitmap with a particular color.

private BitmapDrawable makeColorFillDrawable(int color, Drawable d) {
    Bitmap maskBitmap = ((BitmapDrawable) d).getBitmap();
    final int width = maskBitmap.getWidth();
    final int height = maskBitmap.getHeight();
    maskBitmap = null;

    final Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(outBitmap);
    d.setBounds(0, 0, width, height);
    d.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    d.draw(canvas);
    d.setColorFilter(null);
    d.setCallback(null);

    return new BitmapDrawable(getResources(), outBitmap);
}

保存您从您的服务器在共享preferences取色。然后,子类按钮并应用基于您保存价值的颜色填充绘制。所有你需要做的就是替换<按钮... /方式> 在XML的路径你自定义的

Save the color you fetch from your server in your SharedPreferences. Then subclass Button and apply the color fill drawable based on the value you saved. All you'd need to do then is replace <Button.../> in your XML with the path to your custom one.

下面是使用动作条图标的例子。

Here's an example with the ActionBar icon.

    final Drawable d = getResources().getDrawable(android.R.drawable.sym_def_app_icon);
    getActionBar().setIcon(makeColorFillDrawable(Color.RED, d));

这篇关于Android应用程序:动态地应用色彩主题在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆