如何将 ImageButton 背景颜色设置为可以适应夜间模式的特定主题颜色?

How to set an ImageButton background color to a specific color of a theme that could be adapted to the night mode?

我想找到一种方法将 ImageButton 的背景颜色设置为我在主题中定义的颜色,并且该颜色会随着白天或夜间模式而变化。

这可能吗?

这是主题文件中我想要的颜色 (colorPrimary):

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

非常感谢。

洗礼

现在,Android提供了一个夜间主题,您可以在默认情况下进行编辑,如果您知道的话,

但我在应该创建新样式(主题)的几年前使用这种方式:

** 您的样式或主题文件:**

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->

<style name="Theme.Test" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
</style>

<!-- the Night theme -->
<style name="myNightTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Night brand color. -->
    <item name="colorPrimary">@color/black</item>
</style>

</resources>

在第二个主题中你可以改变里面你想要的任何颜色

您可以在 Activity 中使用此代码将主题更改为第二个:

setTheme(R.style.myNightTheme);

首先,在清单中为您的主题赋予自定义值:

        <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:theme="@style/MyMCNoactionBar">...

然后在 style.xml 中自定义您的主题:

<style name="MyMCNoactionBar" 
parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowContentTransitions" 
tools:targetApi="21">true</item>

    <!-- Colors -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryVariant</item>

    <item name="colorSecondary">@color/colorSecondary</item>
    <item 
name="colorSecondaryVariant">@color/colorSecondaryVariant</item>

    <item name="android:colorBackground">@color/colorBackground</item>
    <item name="colorSurface">@color/colorSurface</item>
    <item name="colorError">@color/colorError</item>

    <item name="colorOnPrimary">@color/colorOnPrimary</item>
    <item name="colorOnSecondary">@color/colorOnSecondary</item>
    <item name="colorOnBackground">@color/colorOnBackground</item>
    <item name="colorOnSurface">@color/colorOnSurface</item>
    <item name="colorOnError">@color/colorOnError</item>
    <item name="android:statusBarColor" 
tools:targetApi="21">@color/colorPrimaryVariant</item>

    <item name="android:textColor">@color/textColor</item>
    <item name="android:divider">@color/divider</item>
    <item 
name="colorControlHighlight">@color/colorControlHighlight</item>

    <item 
name="colorBackgroundFloating">@color/alertDialogBackgroundColor</item>                                  
<!--AlertDialogBackgroundColor-->
    <item name="android:colorBackgroundFloating" 
tools:targetApi="23">@color/alertDialogBackgroundColor</item>
    <item name="colorControlNormal">@color/controlNormal</item>
</style>

然后在您的主 class 中,检查用户对深色或浅色主题的值并将其应用于主题:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    setDayNight(); //Note: before setContentView
    setContentView(R.layout.main_lauout);
}

private void setDayNight() {

    boolean isDark =this.getSharedPreferences("setting", Context.MODE_PRIVATE)
                   .getBoolean("isDark", false);
    if (isDark) {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
}

右键单击 res/ values/colors 目录和(新建)和(值资源文件)

将文件名设置为颜色

将目录命名为 values-night 单击确定

把你的浅色放在colors.xml:

    <color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryVariant">#303F9F</color>

<color name="colorSecondary">#536DFE</color>
<color name="colorSecondaryVariant">#212C75</color>

<color name="colorBackground">#FFFFFF</color>
<color name="colorSurface">#FFFFFF</color>
<color name="colorError">#B00020</color>

<color name="colorOnPrimary">#FFFFFF</color>
<color name="colorOnSecondary">#FFFFFF</color>
<color name="colorOnBackground">#FFFFFF</color>
<color name="colorOnSurface">#000000</color>
<color name="colorOnError">#FFFFFF</color>
<color name="textColor">#000000</color>
<color name="colorAccent">#536DFE</color>
<color name="colorAccentTwo">#536DFE</color>
<color name="divider">#DDcccccc</color>
<!--ImageButton-->
<!--set your light color Instead @color/blue_900 -->
<color name="imageButtonColor">@color/blue_900</color> 

将夜间颜色放入 colors.xml(夜晚)

<color name="colorPrimary">#212121</color>                                      
<!--actionBarColor-->
<color name="colorPrimaryVariant">#000000</color>                               
<!---->

<color name="colorSecondary">#21E600</color>                                    
<!--radioButtonCircleCheckedColor-->
<color name="colorSecondaryVariant">#03DAC6</color>

<color name="colorBackground">#303030</color>                                   
<!--background-->
<color name="colorSurface">#121212</color>
<color name="colorError">#CF6679</color>

<color name="colorOnPrimary">#000000</color>
<color name="colorOnSecondary">#FF26FF00</color>                                
<!--color drawable into fab-->
<color name="colorOnBackground">#FFFFFF</color>                                 <!--ActionBarTitle-->
<color name="colorOnSurface">#FFFFFF</color>                                    <!--circle Radio Button-->
<color name="colorOnError">#000000</color>
<color name="textColor">#FFFFFF</color>                                         
<!--TextColor-->
<color name="colorAccent">#FF26FF00</color>
<color name="colorAccentTwo">#28B133</color>
<color name="divider">#000000</color>
<!--ImageButton-->
<!--set your color dark Instead #424242-->
<color name="imageButtonColor">#424242</color> 

并且在您的布局中,select imageButton 颜色的以下值:

   <ImageButton
app:backgroundTint="@color/imageButtonColor"
    />

上面的方法是Android推荐的方法,但是你也可以按照下面的方法(如果只是想改变视图的颜色,下面的方法更好):

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_lauout);

    boolean isDark = getSharedPreferences("setting", Context.MODE_PRIVATE)
                   .getBoolean("isDark", false);


    if (isDark)
    if (Build.VERSION.SDK_INT >= 21)
        findViewById(R.id.imageButton).getBackground().setTint(getResources().getColor(R.color.yourDarkColor));

}

根据 Material 设计指南(参见 here),

  1. styles.xml 中,继承自 DayNight 主题。
    <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
        ...
        ...
    </style>
  1. values-night目录下新建colors.xml文件。

  1. values\colors.xml,
    <color name="primary_color">{your-light-theme-color-here}</color>

values-night\colors.xml

    <color name="primary_color">{your-dark-theme-color-here}</color>
  1. styles.xml,
    <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
        <item name="colorPrimary">@color/primary_color</item>
        ...
    </style>
  1. 在您的 AndroidManifest.xml
  2. 中设置此主题
    <application
        ...
        android:theme="@style/Theme.MyApp">
    </application>

我觉得这是最符合指南且最有效的方法。