Android 即使在适配器中设置为日间(灯光)模式,DayNight主题仍然遵循系统

Android 即使在适配器中设置为日间(灯光)模式,DayNight主题仍然遵循系统,android,android-appcompat,android-theme,Android,Android Appcompat,Android Theme,我正在我的应用程序中使用DayNight主题。我在两个地方使用相同的适配器类,一个在片段中,一个在活动中。我在适配器中使用的布局有一个遵循DayNight主题的视图。在我的代码中,我从未设置过跟随系统的模式。但是活动适配器中的视图始终遵循系统主题,即使设置为MODE_NIGHT_NO或MODE_NIGHT_YES。我在片段中使用的适配器工作得非常好,遵循应用程序主题,而不是系统主题。 这是我的布局中的视图: <View android:id="@+id/overlay" a

我正在我的应用程序中使用DayNight主题。我在两个地方使用相同的适配器类,一个在片段中,一个在活动中。我在适配器中使用的布局有一个遵循DayNight主题的视图。在我的代码中,我从未设置过跟随系统的模式。但是活动适配器中的视图始终遵循系统主题,即使设置为MODE_NIGHT_NO或MODE_NIGHT_YES。我在片段中使用的适配器工作得非常好,遵循应用程序主题,而不是系统主题。 这是我的布局中的视图:

<View
    android:id="@+id/overlay"
    android:layout_width="@dimen/col3_image_width"
    android:layout_height="@dimen/col3_image_height"
    android:background="@drawable/image_selected_bg"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />    

请注意,
DayNight
主题中的
@color/selectedImage
将引用默认的浅色xml资源。因此,您需要在名为
values night
/
colors.xml
的文件夹中为
@color/selectedImage
创建一个新的xml文件。我假设目前您只有
/
颜色。xml

显示一些代码我添加了代码。看一看。我有两个colors.xml文件。一个是默认的(灯光模式),另一个是带有夜间标记的夜间模式。当我在片段中使用同一个适配器时,它工作得非常好。原因是我作为参数传递给适配器的上下文。我正在传递getApplicationContext(),但我应该传递MyActivity.this。现在它在活动中也可以完美地工作。只是更改了我传递适配器的上下文参数。
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<solid android:color="@color/selectedImage"/>
<corners android:radius="@dimen/rounded_corner_image_radius" />
</shape>
view.setVisibility(View.VISIBLE);