【问题标题】:How to correctly theme an activity and identify what attributes are wrong?如何正确地主题化活动并确定哪些属性是错误的?
【发布时间】:2019-04-29 00:05:13
【问题描述】:

我正在为我的应用程序支持深色/夜间模式, 所以我将我的活动更改为AppCompatActivity 和 android:theme 为一个干净的自定义主题,扩展了“Theme.AppCompat.DayNight”,但在这里和那里发现了很多变化。

我不知道修复它们然后支持暗模式的正确方法是什么。

  1. 如何找出影响哪些属性,例如导航栏颜色、导航图标颜色、操作栏上的向上/后退图标颜色、操作栏背景颜色和活动背景颜色? (目前,我只是尝试错误“android”和“appcompat”中定义的所有属性)

  2. 我正在处理的活动是一个设置活动,也就是 AppCompatActivity 中的 PreferenceFragment,每个首选项都是从自定义布局中扩展而来的,带有以下 ImageView 图标:

    <ImageView
      android:id="@android:id/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal|top"
      android:layout_marginTop="16dip"
      android:layout_marginStart="@dimen/settings_entry_left_margin"/>
    

如您所见,我没有对其设置任何色调,但图标的颜色仍然发生了变化。我应该如何找出原因(并解决它)?

PS。这个问题更多关于 “什么是主题活动的‘正确’方式?” “如何知道哪些属性可以被主题化?”

谢谢。

【问题讨论】:

  • 我只是 CTRL + 单击 styles.xml 中的父级并观察设置了哪些属性。另一种方法是使用 Android Studio 中的主题编辑器。

标签: android attributes styles themes


【解决方案1】:

App战斗属性列表可见here。过去我尝试做类似的事情,但我并不一定要覆盖其中的每一个。我在屏幕上转来转去,查看我想要更改的内容,然后只对其进行编辑。

【讨论】:

    【解决方案2】:

    您必须在 res 文件夹的样式文件中定义主题。下面是设置主题样式的代码

    <resources>
    
    <style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="titleColor">@color/colorPrimary</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccentLight</item>
        <item name="secondBackground">#c2c2c2</item>
        <item name="roundButton">@drawable/round_button_light</item>
        <item name="alertDialogTheme">@style/AppThemeDialogLight</item>
        <item name="android:itemBackground">#ffffff</item>
        <item name="android:textColor">#000000</item>
    
    </style>
    
    <style name="AppThemeDialogLight" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccentLight</item>
    </style>
    
    <style name="AppThemeLight.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:itemBackground">#ffffff</item>
        <item name="android:textColor">#000000</item>
    </style>
    
    <style name="AppThemeLight.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
    <style name="AppThemeLight.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    
    <style name="AppThemeDark" parent="Theme.AppCompat">
        <item name="titleColor">@android:color/primary_text_dark</item>
        <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
        <item name="colorPrimary">@color/primary_material_dark</item>
        <item name="colorAccent">@color/colorAccentDark</item>
        <item name="secondBackground">#151515</item>
        <item name="roundButton">@drawable/round_button_dark</item>
        <item name="alertDialogTheme">@style/AppThemeDialogDark</item>
        <item name="android:itemBackground">@color/item_theme_dark</item>
        <item name="android:textColor">#ffffff</item>
    </style>
    
    <style name="AppThemeDialogDark" parent="@style/Theme.AppCompat.Dialog.Alert">
        <item name="colorAccent">@color/colorAccentDark</item>
    </style>
    
    <style name="AppThemeDark.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:itemBackground">@color/item_theme_dark</item>
        <item name="android:textColor">#ffffff</item>
    </style>
    

    颜色.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#008577</color>
        <color name="colorPrimaryDark">#00574B</color>
        <color name="colorAccent">#00897B</color>
        <color name="white">#fff</color>
        <color name="colorA">#D81B60</color>
        <color name="secondBackground">#33333333</color>
        <color name="colorAccentLight">#FF4081</color>
        <color name="colorAccentDark">#a4a4a4</color>
        <color name="dark_blue">#0d47a1</color>
        <color name="light_theme_dark_contextual_bar_background">@color/dark_blue</color>
        <color name="dark_theme_elapsed_time_paused_color">#4cffffff</color>
        <color name="tooltip_background_dark">#e6616161</color>
        <color name="tooltip_background_light">#e6ffffff</color>
        <color name="dark_theme_color_primary_dark">#040404</color>
        <color name="tab_background_selected_theme_dark">#232323</color>
        <color name="tab_background_unselected_theme_dark">#232323</color>
        <color name="item_theme_dark">#363636</color>
    </resources>
    

    在活动的 onCreateMethod 中将主题设置为

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAppTheme(getTheme(this, R.style.AppThemeLight_NoActionBar, R.style.AppThemeDark_NoActionBar));
        setContentView(R.layout.activity_main);
    }
    
    
    public static int getTheme(Context context, int light, int dark) {
            final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context);
            String theme = shared.getString(PREFERENCE_THEME, "");  // upon selecting the theme from options just put the related string in the shared prefs. 
            if (theme.equals("Theme_Dark")) {
                return dark;
            } else {
                return light;
            }
        }
    

    还有styles.xml v21

    <resources>
    <style name="AppThemeLight.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
    <style name="AppThemeDark.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:itemBackground">@color/item_theme_dark</item>
        <item name="android:textColor">#ffffff</item>
    
    </style>
    

    主题样式中的这两行基本上做了很多改变

     <item name="android:itemBackground">#ffffff</item>
        <item name="android:textColor">#000000</item>
    

    这两行将使每个视图的背景为白色,每个文本颜色将变为黑色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多