从theme中获取item配置信息的技巧

2017-08-16  本文已影响0人  CarlWu_S

需求:
项目制定了一个theme


image.png

theme中有一个item为"bottomcolor"

image.png

我们需要在java代码中得到这个"bottomcolor"的值,可以用如下方法:
首先在attr.xml中创建一个属性资源

image.png

里面申明"bottomcolor"的属性

image.png

java代码中可以借助这个attr来访问theme资源:

        TypedValue bottomColor = new TypedValue();
        Resources.Theme theme = getTheme();
        theme.resolveAttribute(R.attr.bottomcolor, bottomColor, true);
        Resources resources = getResources();
        if(tab!=null) {
            tab.setBackgroundColor(resources.getColor(bottomColor.resourceId));
        }
上一篇 下一篇

猜你喜欢

热点阅读