问题描述
我对这个话题有些怀疑。
我只使用了 android 为我们提供的 API 的夜间/白天主题,而您为两者设置了主题。然后它适用于 setNightMode() 和类似的一切。 我当时所做的是在视图的背景中设置颜色,当设置为夜间主题时它会发生变化
所以我想知道例如我是否创建了 3 个这样的主题(浅色/深色/蓝色)(我不想重复代码,所以我将显示 1 个主题):
<style name="Theme.Blue" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryVariant">@color/greentwo</item>
<item name="colorOnPrimary">@color/reed</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
然后当我进入一个活动/片段时:
同样的背景也有相同颜色的高低色调等的对比。
我是否必须继承任何主题,将其设置在 xml 的顶部还是将其 1 对 1 设置为每个视图或我不知道的任何内容?
-
那么我该怎么做,推荐的方法是什么?
-
我不太明白 colorPrimary、colorPrimaryVariant 和这些东西的去向。例如,在创建警报对话框时,按钮设置为绿色!
关于管理当前用户首选项主题不是问题,问题在于将其应用于所有视图的样式。
解决方法
拥有不同主题的全部意义在于不必以编程方式更改所有内容。所以你需要针对不同的主题有不同的风格:
<resources>
<!-- Base application theme. -->
<!-- Light theme. -->
<style name="LightMode" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary2</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark2</item>
<item name="colorAccent">@color/colorAccent2</item>
<item name="color_text">@color/color_text2</item>
</style>
<!-- Dark theme. -->
<style name="NightModeTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="color_text">@color/color_text</item>
</style>
</resources>
在您的 attr.xml
中也有不同的属性:
<resources>
<declare-styleable name="Theme">
<attr name="colorPrimary" format="color"/>
<attr name="colorPrimaryDark" format="color"/>
<attr name="colorAccent" format="color"/>
<attr name="color_text" format="color"/>
...
</declare-styleable>
</resources
在您的 colors.xml
中设置不同的颜色:
<resources>
<!-- Dark theme colors. -->
<color name="colorPrimary">#353535</color>
<color name="colorPrimaryDark">#252525</color>
<color name="colorAccent">#FF3C00</color>
<color name="color_text">#FFFFFF</color>
<!-- Light theme colors. -->
<color name="colorPrimary2">#FFFFFF</color>
<color name="colorPrimaryDark2">#FFBB29</color>
<color name="colorAccent2">#FFB728</color>
<color name="color_text2">#272727</color>
</resources>
然后在 xml 文件的视图中,您将从 attr
文件而不是 colors
文件中获取颜色,如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read me"
android:textColor="?attr/color_text"/>
此外,如果您想为每个活动设置主题,请确保在设置内容视图之前进行设置。
你可以有一个这样的类:
abstract class ThemeManager {
public static void set(Context context,String activeTheme) {
int themeRecourseID = R.style.LightMode;
if (activeTheme.equals("DarkMode")) {
themeRecourseID = R.style.DarkMode;
}
context.setTheme(themeRecourseID);
}
}
并以这种方式使用它:
override fun onCreate(savedInstanceState: Bundle?) {
//SET THE THEME HERE BEFORE SETTING THE CONTENTVIEW OF THE ACTIVITY
ThemeManager.set(this,"DarkMode");
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}