美文网首页
从theme中获取item配置信息的技巧

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

作者: CarlWu_S | 来源:发表于2017-08-16 09:47 被阅读0次

需求:
项目制定了一个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));
        }

相关文章

网友评论

      本文标题:从theme中获取item配置信息的技巧

      本文链接:https://www.haomeiwen.com/subject/ymnbrxtx.html