38

私はこのようなことを試しましたが、行き詰まりました:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
  // how to get color?
}
4

3 に答える 3

66

次の方法で、現在のテーマから背景色 (または Drawable) を取得できます。

TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.isColorType()) {
    // windowBackground is a color
    int color = a.data;
} else {
    // windowBackground is not a color, probably a drawable
    Drawable d = activity.getResources().getDrawable(a.resourceId);
}

isColorTypeは API レベル 29 で導入されました。それ以前は、代わりに以下を使用できます。

if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT)
于 2013-01-22T21:09:19.940 に答える
6

以下を使用して、テーマのリソースを取得できます。

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});     
int attributeResourceId = a.getResourceId(0, 0);
于 2012-09-11T23:26:17.630 に答える