在colors.xml中设置的styles.xml中更改颜色



我在下面的colors.xml中设置了自定义颜色。但是,可以按照下面的styles.xml主题节点更改/更新颜色;

colors.xml

<resources>
    <color name="themeBackground">#000000</color>
    <color name="darkColor">#000000</color>
    <color name="lightColor">#ffffff</color>
<resources>

并在我的 v21 styles.xml 中手动更改,如

中使用
//used in activity
onCreate().setTheme(R.style.DarkTheme);

v21 styles.xml

<style name="DarkTheme">
    <!-- change themeBackground manually here -->
    <item name="themeBackground">@color/darkColor</item>
</style>
<style name="LightTheme">
    <!-- change themeBackground manually here -->
    <item name="themeBackground">@color/lightColor</item>
</style>

我已经尝试过,但没有运气,因为似乎只能更改Android值,即

<item name="colorPrimary">@color/lightColor</item>

具有成功(未完全测试)通过下面使用 values themes.xml &amp; values attr.xml values colors.xml

*Ensure any themes created in valuesstyles.xml are not duplicated in themes.xml (i.e same key name <style name="LightLoginThemeV3">)*

在下面添加到 values colors.xml (应该已经存在...)

<!-- light Theme colors -->
<color name="lightBackgroundPrimaryV3">@color/white</color>
<color name="lightBackgroundSecondaryV3">@color/lighter_grey</color>
<color name="lightBackgroundAltV3">@color/lighter_grey</color>
<color name="lightBackgroundAltAlphaV3">#809E9E9E</color>
<color name="lightBackgroundLightV3">#80FFFFFF</color>
<color name="lightThemePrimaryColourV3">#01aac4</color>
<color name="lightThemeSecondaryColourV3">#025f8b</color>
<color name="lightThemeDarkPrimaryColourV3">@color/white</color>
<color name="lightThemeDarkSecondaryColourV3">@color/lighter_grey</color>
<!-- dark Theme colors -->
<color name="darkBackgroundPrimaryV3">#241e45</color>
<color name="darkBackgroundSecondaryV3">#2f2856</color>
<color name="darkBackgroundAltV3">#483e81</color>
<color name="darkBackgroundAltAlphaV3">#80483e81</color>
<color name="darkBackgroundLightV3">#f4f3f3</color>
<color name="darkThemePrimaryColourV3">#01aac4</color>
<color name="darkThemeSecondaryColourV3">#025f8b</color>
<color name="darkThemeDarkPrimaryColourV3">#2f2856</color>
<color name="darkThemeDarkSecondaryColourV3">#50448f</color>

values attr.xml (如果不存在的(如果不存在),请在值文件夹中创建attr.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- background color keys -->
    <attr name="basePrimaryBackgroundColour" format="reference|color"/>
    <attr name="baseSecondaryBackgroundColour" format="reference|color"/>
</resources>

values themes.xml (如果不存在,则在值文件夹中创建attr.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LightLoginThemeV3">
        <!-- keys from values/attrs.xml file -->
        <!-- background color keys -->
        <!-- using light Theme colors via @color -->
        <item name="basePrimaryBackgroundColour">@color/lightBackgroundPrimaryV3</item>
        <item name="baseSecondaryBackgroundColour">@color/lightBackgroundSecondaryV3</item>
        <item name="windowNoTitle">true</item>
        <!-- Support library compatibility -->
        <item name="windowActionBar">false</item>
        <item name="android:windowBackground">@color/backgroundSecondaryV3</item>
        <item name="android:popupBackground">@color/backgroundSecondaryV3</item>
        <!-- EditText Fields -->
        <item name="colorControlNormal">@color/medium_grey</item>
        <item name="colorControlActivated">@color/themePrimaryColourV3</item>
        <item name="colorControlHighlight">@color/themePrimaryColourV3</item>
    </style>
    <style name="DarkLoginThemeV3">
        <!-- keys from values/attrs.xml file -->
        <!-- background color keys -->
        <!-- using dark Theme colors via @color -->
        <item name="basePrimaryBackgroundColour">@color/darkBackgroundPrimaryV3</item>
        <item name="baseSecondaryBackgroundColour">@color/darkBackgroundSecondaryV3</item>
        <item name="android:windowNoTitle">true</item>
        <!-- Support library compatibility -->
        <item name="windowActionBar">false</item>
        <item name="android:windowBackground">@color/colorTrans</item>
        <item name="android:popupBackground">@color/darkist_grey</item>
        <!-- EditText Fields -->
        <item name="colorControlNormal">@color/dark_grey</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
        <item name="colorControlHighlight">@color/colorPrimary</item>
    </style>
</resources>

以及在我相关的布局文件中,将背景设置为So ...

 //key from values/attrs.xml file
 android:background="?attr/basePrimaryBackgroundColour"

以及在我的活动oncreate()方法(和onresume()方法)中,从值 themes.xml设置主题。

if(darkTheme){
    setTheme(R.style.DarkLoginThemeV3);
}else{
    setTheme(R.style.LightLoginThemeV3);
}

在我的活动中,通过clickListner事件,Value themes.xml通过下面的vive themes.xml变化...

if(darkTheme){
    setTheme(R.style.DarkLoginThemeV3);
    recreate();
}else{
    setTheme(R.style.LightLoginThemeV3);
    recreate();
}

有多种方法可以在运行时应用主题。其中一个在这里被很好地覆盖了。这些方法的问题在于,您必须通过调用recreate()至少重新创建活动。但是您可能会遇到一种不可行的场景,可能是由于它依赖于现有建筑模式的数据或效率低下的情况。

也没有重新创建活动的其他方法也可以正常工作:递归找到观点和应用主题。为此,第一个约束是具有所有自定义视图。

对于每个框架视图或ViewGroup,定义自定义视图如下以下示例:

class CustomTextView extends AppCompatTextView {}

现在您需要一个类似于下面的示例的界面,可以在视图中暗示并在动态更改主题时触发。

interface Painter {
  void applyTheme(int theme)
}

在您的自定义视图类中实现它:

class CustomTextView extends AppCompatTextView implements Painter {
     @Override
     public void applyTheme(int theme) {
          switch (theme) { ... }
     }
}

以及您的活动中的某个地方,递归找到实现Painter并触发applyTheme的视图如下:

public void onChangeThemeClick(int selectedTheme){
     View rootView = findViewById(android.R.id.content);
     paintRecurively(rootView,selectedTheme);
}
public void paintRecurively(View view, int theme) {
   //if view implements painter, trigger the method
   if(view instanceof Painter){
     (Painter)view.applyTheme(theme);
   }
   //if view is viewgroup then further call this method for its children.
   if(view instanceof ViewGroup){
      ViewGroup vg = (ViewGroup)view;
      for(...childCount of vg){
          paintRecurively(vg.getChildAt(i),theme);
      }
   }
}

这听起来像是很多工作。但是,只有在您不想重新创建活动时才必须这样做。

最新更新