需求:
項(xiàng)目制定了一個(gè)theme
theme中有一個(gè)item為"bottomcolor"
我們需要在java代碼中得到這個(gè)"bottomcolor"的值,可以用如下方法:
首先在attr.xml中創(chuàng)建一個(gè)屬性資源
里面申明"bottomcolor"的屬性
java代碼中可以借助這個(gè)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));
}