android - 如何在Android中设置不同的主题和最佳方式

标签 android android-view android-theme android-styles

我对这个话题有一些疑问。

我只使用 android 为其 API 提供的夜间/白天主题,并且您为两者设置主题。然后它适用于 setNightMode() 和类似的东西。 然后我所做的是在 View 背景中设置颜色,当设置为夜间主题时它会发生变化

所以我想知道我是否像这样创建 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>

然后当我进入 Activity/fragment 时:

  1. 我检查用户设置的首选主题是什么,例如看到的主题是Theme.Blue

  2. 因此,对于这个主题,我想加载一些绿色文本和其他红色文本,所以我的想法是以编程方式设置主题。 TextView.setAppareance(Theme style) 这似乎是一项非常艰苦的工作。

同样的情况也适用于背景,其中存在相同颜色的较高和较低色调的对比等等。

我是否必须继承任何主题,将其设置在 xml 顶部,或者将其 1 对 1 设置到每个 View 或我不知道的任何内容?

  • 那么我该如何做到这一点,推荐的方法是什么?

  • 我不太明白 colorPrimary、colorPrimaryVariant 之类的东西去哪里了。例如,在创建警报对话框时,按钮设置为绿色!

关于管理当前用户首选项主题不是问题,问题在于将其应用到所有 View 的样式。

最佳答案

拥有不同主题的全部意义在于不必以编程方式更改所有内容。所以你需要针对不同的主题有不同的样式:

<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 文件的 View 中,您将从 attr 文件获取颜色,而不是 colors 文件,如下所示:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Read me"
    android:textColor="?attr/color_text"/>

此外,当您想为每个 Activity 设置主题时,请确保在设置内容 View 之前执行此操作。

你可以有这样的类(class):

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)

}

关于android - 如何在Android中设置不同的主题和最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65584542/

相关文章:

安卓 8 : Can't change ringer mode to non-silent when ringer mode is silent

安卓 GooglePlus API 错误

android - Maven 使用带有 ADT 的 Android 库项目

android - 自定义View不随手势移动

java - 在 Android 中使用 Java 进行抽象

Android View vs SurfaceView vs GLSurfaceView for 2D drawing app with Zoomable User Interface

android - 应用不同颜色的样式

android - 以一个主题而非另一个主题的自定义样式覆盖 Android 背景

android - 有人可以解释一下attr吗?

android - Android 4.0 上 Holo 主题的默认颜色值是多少?