从TypedValue中提取背景Color/Drawable

Fem*_*emi 0 resources android styles

我正在开发一个Android项目,并尝试检索窗口标题背景样式,然后提取背景颜色/ drawable(这样我就可以将它们应用到我的自定义标题栏布局).我已经能够使用此代码获得样式的TYPE_REFERENCE:

    TypedValue a = new TypedValue();
            getTheme().resolveAttribute(android.R.attr.windowTitleBackgroundStyle, a, true);
            if(a.type == TypedValue.TYPE_REFERENCE){
    // GOES IN HERE
// Drawable d = getResources().getDrawable(a.resourceId); // THROWS A RESOURCES NOT FOUND EXCEPTION
    }
Run Code Online (Sandbox Code Playgroud)

关于如何成功地弄清楚指向的资源是什么以及从那里到颜色/抽屉,我即将空白(尽管在Google中轻松进行了2个小时的拼写).我假设它是XML中定义的样式:如何以编程方式实际获取该样式的属性/属性/设置并获取背景颜色/ Drawable?

Fem*_*emi 5

啊,想通了:

TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowTitleBackgroundStyle, a, true);
        if(a.type == TypedValue.TYPE_REFERENCE){
            TypedArray b = this.obtainStyledAttributes(a.resourceId, new int[]{
                    android.R.attr.background
            });
            b.getValue(0, a);
                        titleBar.setBackgroundResource(a.resourceid);
            b.recycle();
        }
Run Code Online (Sandbox Code Playgroud)