在 Android 片段中获取主题的主要颜色
我在片段中使用确定的循环进度指示器。 根据进度,进度指示器应具有不同的颜色。所以,如果进度是100%,它会变成红色,否则它会变成蓝色。
在之前的项目中,我成功地使用了以下代码,但它不再起作用 - 而且我找不到它的原因。
该代码最初取自这篇文章: 以编程方式获取颜色值这是一个参考(主题)
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getContext().getTheme();
@ColorInt int color;
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
color = typedValue.data;
circularProgressIndicator.setIndicatorColor(color);
我使用的 R 类是正确的 -->导入mypackage.R;
设置正确(甚至太频繁!?)
- 清单文件中
- 布局 XML 中的
- “setContentView”之前的主活动
- 主题在片段布局 XML 中
有人知道为什么我无法从主题中选择颜色吗?这样吗?
我无法直接从 R.color 设置颜色,因为它取决于白天/夜间模式。 另一种方法是在代码中为进度指示器设置不同的样式,但 CircularProgressIndicator 类似乎没有像 TextView 那样提供此功能。
颜色 XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ReminderBlue">#00337A</color>
<color name="ReminderError">#B00020</color>
...
</resources>
Theme.XML
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.Reminder" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/ReminderBlue</item>
<item name="colorError">@color/ReminderError</item>
.....
</style>
FragmentLayout.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:theme="@style/Theme.Reminder"
tools:context=".activities.fragments.MainFragment">
I am using a determinate circular progress indicator in my Fragment.
Depending on the progress, the progress indicator shall have different colors. So, if the progress is 100%, it shall turn red, otherwise it is blue.
In a previous project I successfully used the following code, which is not working anymore - and I cannot find the reason for it.
The code was originally taken from this post: Get color value programmatically when it's a reference (theme)
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getContext().getTheme();
@ColorInt int color;
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
color = typedValue.data;
circularProgressIndicator.setIndicatorColor(color);
The error already pops up during building.
The R class I am using is the right one --> import mypackage.R;
The theme is set properly (or even too often!?)
- in the Manifest file
- in the Main Activity Layout XML
- in the Main Activity before "setContentView)
- in the Fragment Layout XML
Does anybody have an idea why I cannot select the color from my theme this way?
I cannot set the the color directly from R.color as it depends on day/night mode.
An other approach would be to set different styles for the progress indicator in code, but it seems that the class CircularProgressIndicator is not offering this as for example a TextView does.
Colors XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ReminderBlue">#00337A</color>
<color name="ReminderError">#B00020</color>
...
</resources>
Theme.XML
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.Reminder" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/ReminderBlue</item>
<item name="colorError">@color/ReminderError</item>
.....
</style>
FragmentLayout.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:theme="@style/Theme.Reminder"
tools:context=".activities.fragments.MainFragment">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论