[英]how to fix image view icon color changes in dark mode and lite mode of the device
Hello im trying to include icons in imageview but when the dark mode is on the color is dark and when on lite mode its shows in white , i want color to be white in both modes你好,我试图在 imageview 中包含图标,但是当暗模式处于暗模式时,颜色是暗的,而在精简模式时,它显示为白色,我希望两种模式下的颜色都为白色
vector // icon矢量// 图标
<vector android:height="24dp" android:tint="@color/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>
ImageView // icon included as background ImageView // 作为背景包含的图标
<ImageView
android:id="@+id/backArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/ic_baseline_arrow_back_24"
android:contentDescription="@string/todo"
android:elevation="10dp" />
themes.xml // litemod/normal的themes.xml // litemod /正常
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyAppNotFinal" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#FFFFFFFF</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/lite_grey</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">@color/black</item>
<!-- Customize your theme here. -->
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
</resources>
themes.xml // dark mod/night themes.xml // 黑暗模组/夜晚
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyAppNotFinal" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#FFFFFFFF</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
</resources>
colors.xml颜色文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#000000</color>
<color name="white">#FFFFFFFF</color>
<color name="grey">#181818</color>
<color name="lite_grey">#e2e2e2</color>
<color name="dark_red">#bd081c</color>
<color name="green">#0B5B37</color>
</resources>
Ok i got an answer to my question, acttualy my app already has dark elements and when i add a white background to an imageview or some more elements but it changes to dark grey in the dark mod of the device which i dont want ,so i got an solution to this issue好的,我得到了我的问题的答案,实际上我的应用程序已经有深色元素,当我向图像视图或更多元素添加白色背景时,但它在我不想要的设备的深色模式中变为深灰色,所以我得到了这个问题的解决方案
Step 1第1步
Delete the night/dark theme.xml in the values删除值中的 night/dark theme.xml
Step 2第2步
In the normal/light theme.xml in the values change the在 normal/light theme.xml 中的值更改
<style name="Theme.MyAppNotFinal" parent="Theme.MaterialComponents.DayNight.NoActionBar">
to到
<style name="Theme.MyAppNotFinal" parent="Theme.MaterialComponents.Light.NoActionBar">
Step 3第 3 步
In the same theme.xml // light/normal theme在同一个theme.xml // 浅色/普通主题
add this line添加这一行
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
inside the style在风格里面
like this像这样
<style name="Theme.MyAppNotFinal" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
this method worked for me if anyone still have some issues please check the bellow question it has many answers hope it will work for you这个方法对我有用,如果有人还有问题,请检查下面的问题,它有很多答案希望它对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.