【问题标题】:How to set different themes and best way in Android如何在 Android 中设置不同的主题和最佳方式
【发布时间】:2021-01-05 18:28:42
【问题描述】:

我对这个话题有些疑惑。

我只使用了 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>

然后当我进入一个活动/片段时:

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

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

同样的情况也适用于与相同颜色的较高和较低色调等形成对比的背景。

我是否必须继承任何主题,将其设置在 xml 的顶部或将其 1 逐 1 设置为每个视图或我不知道的任何内容?

  • 那么我该怎么做呢,推荐的方法是什么?

  • 我不完全了解 colorPrimary、colorPrimaryVariant 和这些东西的去向。例如,在创建警报对话框时,按钮设置为绿色!

关于管理当前用户偏好主题不是问题,问题在于将其应用于所有视图的样式。

【问题讨论】:

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


    【解决方案1】:

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

    <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)
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 2017-07-07
      • 1970-01-01
      • 2020-09-28
      • 2015-01-14
      • 2017-08-07
      • 2018-08-10
      • 2011-11-15
      相关资源
      最近更新 更多