我想为我的 android 应用程序实现夜间模式,所以我使用 Theme.AppCompat.DayNight 主题来实现夜间模式。但是我必须在夜间模式下自定义工具栏和回收站视图的颜色。
为此,我在 attrs.xml 文件中声明了该属性,并在 recyclerview 中使用该属性作为背景。
这是 attrs.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ds">
<attr name="rv_color" format="color"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是回收站视图
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/rv_color"
android:overScrollMode="never"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Run Code Online (Sandbox Code Playgroud)
现在对于样式,我已经为夜间模式声明了styles.xml 和styles.xml(夜间)。
这是styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@android:color/white</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorPrimary">@color/colorPrimary</item>
<item name="android:windowDisablePreview">false</item>
<item name="rv_color">#FF0000</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是styles.xml(晚上)
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@android:color/white</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item …
Run Code Online (Sandbox Code Playgroud) android android-styles android-recyclerview android-night-mode android-dark-theme
我正在研究当前 Android 应用程序中的 CardView 和深色主题
当启用暗模式时,除了 CardViews 之外,我的大部分应用程序都按预期工作。
我的 Gradle 和这个很像
android {
compileSdkVersion 29
defaultConfig {
applicationId "org.application.investigation"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
implementation 'androidx.cardview:cardview:1.0.0'
Run Code Online (Sandbox Code Playgroud)
我的主题如下:-
<resources>
<style name="AppTheme" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorError">@color/design_default_color_error</item>
<item name="android:textColor">?attr/colorOnBackground</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Run Code Online (Sandbox Code Playgroud)
我的 CardView 布局类似于:-
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_films_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="?android:attr/colorBackground"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true"
tools:context=".films.Films">
<LinearLayout
android:id="@+id/item_films_film" …
Run Code Online (Sandbox Code Playgroud) android android-cardview material-components-android android-dark-theme
我尝试删除 night theme.xml
,并尝试在 night 主题中进行颜色更改。我也试过
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Run Code Online (Sandbox Code Playgroud)
但这些都没有奏效。
这是我的theme.xml
:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.app" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/light_blue</item>
<item name="colorPrimaryVariant">@color/light_blue</item>
<item name="colorOnPrimary">@color/white</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>
</resources>
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
某些设备(例如Poco F2 Pro)可以在与深色主题不兼容的应用程序中强制使用深色主题。
例如,我的应用程序具有此主题并且与深色主题不兼容:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
这是我的清单应用程序部分,未启用深色主题:
<application
android:name=".helpers.CustomApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)
问题是像Poco F2 Pro这样的设备在应用程序中强制使用深色主题时,会使我的应用程序以不正确和异常的颜色显示,从而破坏 UI。
可以避免吗?
如何强制黑暗主题:https : //www.androidauthority.com/android-q-force-dark-mode-984090
问题是:
当我打开应用程序时,我根据设备的主题设置主题。在android代码中它是= mode_night_follow_system。
让我们想象一下系统(设备)的主题是黑暗的。当登录移动应用程序时,我这样做
AppCompatDelegate.setDefaultNightMode(mode_night_follow_system)
Run Code Online (Sandbox Code Playgroud)
该应用程序将颜色更改为深色,但感觉它并没有采用我在深色中指示的颜色。
该问题仅适用于 Android 10+ 的小米手机
在三星、华为等其他机型上没有问题。
问题是什么?