Set theme color dynamically

后端 未结 5 1443
北荒
北荒 2020-12-14 23:31

I am using themes (dynamically) in my android app, like this:

my_layout.xml (extract):



        
5条回答
  •  清歌不尽
    2020-12-15 00:08

    I have finally done it using following method:

    public static int getColor(String colorName) {
        Context ctx = getContext();
        switch (sTheme) {
            default:
            case THEME_DEFAULT:
                return ctx.getResources().getIdentifier("BLUE_" + colorName, "color", ctx.getPackageName());
            case THEME_BLUE:
                return ctx.getResources().getIdentifier("BLUE_" + colorName, "color", ctx.getPackageName());
            case THEME_GREEN:
                return ctx.getResources().getIdentifier("GREEN_" + colorName, "color", ctx.getPackageName());
        }
    }
    

    This returns color according to my theme (I used prefixes).

提交回复
热议问题