gpt4 book ai didi

android - 如何以编程方式从当前主题获取背景颜色

转载 作者:IT老高 更新时间:2023-10-28 21:50:47 26 4
gpt4 key购买 nike

我尝试过这样的事情,但我卡住了:

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

最佳答案

您可以通过以下方式从当前主题中获取背景颜色(或 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)

关于android - 如何以编程方式从当前主题获取背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12375766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com