我试过这样的东西,但我卡住了:
TypedValue typedValue = new TypedValue();
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
// how to get color?
}
Run Code Online (Sandbox Code Playgroud)
ash*_*hes 57
您可以通过以下方式从当前主题获取背景颜色(或Drawable):
TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// windowBackground is a color
int color = a.data;
} else {
// windowBackground is not a color, probably a drawable
Drawable d = activity.getResources().getDrawable(a.resourceId);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用来获取主题的资源:
TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});
int attributeResourceId = a.getResourceId(0, 0);
Run Code Online (Sandbox Code Playgroud)
对于您的 qoustion,最简单的方法是:
TypedValue typedValue = new TypedValue();
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
// how to get color?
int colorWindowBackground = typedValue.data;// **just add this line to your code!!**
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23896 次 |
| 最近记录: |