在换肤时,先在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,所以多做了一步处理

相关文章:

  • 2022-12-23
  • 2021-07-01
  • 2022-01-04
  • 2022-12-23
  • 2022-01-15
  • 2021-10-23
  • 2022-12-23
猜你喜欢
  • 2022-01-30
  • 2021-09-16
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案