如何动态获取Android应用程序主题背景色
我需要将主题背景色应用于TextView背景色。如果此处描述了流程位,则此文本字段用于为用户选择字体颜色。目前它是透明的。这就是我迄今为止所尝试的:如何动态获取Android应用程序主题背景色,android,android-layout,background-color,Android,Android Layout,Background Color,我需要将主题背景色应用于TextView背景色。如果此处描述了流程位,则此文本字段用于为用户选择字体颜色。目前它是透明的。这就是我迄今为止所尝试的: TypedValue typedValue=new TypedValue(); getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true); if(typedValue.t
TypedValue typedValue=new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true);
if(typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT){
backgroundColor=typedValue.data;
fontColorText=(TextView) findViewById(R.id.textValue);
fontColorText.setBackgroundColor(backgroundColor);
}
但这对我不起作用
谁能建议我如何动态地获得主题颜色 MainActivity.java:
activity_main.xml:
输出:
我在寻找解决同样问题的方法时偶然发现了这篇文章。你的问题帮助我得出了这个答案:
TypedValue themeBackgroundColor = new TypedValue();
int backgroundColor;
if (getActivity().getTheme().resolveAttribute(android.R.attr.colorBackground,
themeBackgroundColor, true)) {
switch (themeBackgroundColor.type) {
case TypedValue.TYPE_INT_COLOR_ARGB4:
backgroundColor = Color.argb(
(themeBackgroundColor.data & 0xf000) >> 8,
(themeBackgroundColor.data & 0xf00) >> 4,
themeBackgroundColor.data & 0xf0,
(themeBackgroundColor.data & 0xf) << 4);
break;
case TypedValue.TYPE_INT_COLOR_RGB4:
backgroundColor = Color.rgb(
(themeBackgroundColor.data & 0xf00) >> 4,
themeBackgroundColor.data & 0xf0,
(themeBackgroundColor.data & 0xf) << 4);
break;
case TypedValue.TYPE_INT_COLOR_ARGB8:
backgroundColor = themeBackgroundColor.data;
break;
case TypedValue.TYPE_INT_COLOR_RGB8:
backgroundColor = Color.rgb(
(themeBackgroundColor.data & 0xff0000) >> 16,
(themeBackgroundColor.data & 0xff00) >> 8,
themeBackgroundColor.data & 0xff);
break;
default:
throw new RuntimeException("ClassName: couldn't parse theme " +
"background color attribute " + themeBackgroundColor.toString());
}
} else {
throw new RuntimeException("ClassName: couldn't find background color in " +
"theme");
}
我验证了colorBackground是Material、Holo和pre Holo主题中的一个指定属性。此外,由于似乎不能保证主题的背景是以AARRGGBB格式指定的,因此我在switch语句中加入了所有有效的格式。我已经检查了所有颜色的箱子。希望有帮助。我想你误解了我的问题。我不需要添加主题。我需要的是根据当前主题背景色更改文本字段背景色。顺便说一句,感谢您的支持。您需要更改textView背景色吗?是的,我需要将文本字段背景颜色更改为与应用程序背景中的颜色相同。请检查答案。这将对您有所帮助您将文本字段背景颜色硬编码为ForegroundColorSpanColor.BLUE。但我需要的是我在问题中描述的,即使在应用程序运行时主题发生了变化,也要始终在背景中更改与主题颜色相同的颜色。我也在寻找解决方案。我还没找到…@Mgamerz不,没有yet@Mgamerz你觉得这个问题有用吗?遗憾的是没有:我仍然需要一个解决方案。。。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="57dp"
android:text="TextView" />
TypedValue themeBackgroundColor = new TypedValue();
int backgroundColor;
if (getActivity().getTheme().resolveAttribute(android.R.attr.colorBackground,
themeBackgroundColor, true)) {
switch (themeBackgroundColor.type) {
case TypedValue.TYPE_INT_COLOR_ARGB4:
backgroundColor = Color.argb(
(themeBackgroundColor.data & 0xf000) >> 8,
(themeBackgroundColor.data & 0xf00) >> 4,
themeBackgroundColor.data & 0xf0,
(themeBackgroundColor.data & 0xf) << 4);
break;
case TypedValue.TYPE_INT_COLOR_RGB4:
backgroundColor = Color.rgb(
(themeBackgroundColor.data & 0xf00) >> 4,
themeBackgroundColor.data & 0xf0,
(themeBackgroundColor.data & 0xf) << 4);
break;
case TypedValue.TYPE_INT_COLOR_ARGB8:
backgroundColor = themeBackgroundColor.data;
break;
case TypedValue.TYPE_INT_COLOR_RGB8:
backgroundColor = Color.rgb(
(themeBackgroundColor.data & 0xff0000) >> 16,
(themeBackgroundColor.data & 0xff00) >> 8,
themeBackgroundColor.data & 0xff);
break;
default:
throw new RuntimeException("ClassName: couldn't parse theme " +
"background color attribute " + themeBackgroundColor.toString());
}
} else {
throw new RuntimeException("ClassName: couldn't find background color in " +
"theme");
}