Loading

获取在attr.xml中声明的主题样式

在换肤时,先在attr.xml中定义Resource的属性名,再到stytle中,根据不同的主题,给属性赋值。

在布局文件中可直接以  android:background="?attr/app_bg"  引用。

在代码中则以以下方式获取资源
    /** 获取attr的资源 */
    public int getResIdOfAttr(int attr){
        TypedValue typeValue = new TypedValue();
        getTheme().resolveAttribute(attr, typeValue, true);
        return typeValue.resourceId;
    }
    
    /** 获取attr的color */
    public int getColorOfAttr(int attr){
        TypedValue typeValue = new TypedValue();
        getTheme().resolveAttribute(attr, typeValue, true);
        return getResources().getColor(typeValue.resourceId);
    }

其中获取color时,因为View的setColor(int color)中传入的不是资源Id,所以多做了一步处理

posted @ 2017-01-02 16:01  辉灬  阅读(376)  评论(0编辑  收藏  举报