我正在实现对暗模式的支持。但是在我将 Theme parent 更改为Theme.AppCompat.DayNight 之后,Action Bar 中的文本是黑色的(白天模式下为黑色,黑暗模式下为白色)。我希望文本始终为白色。我试图在styles.xml (values and values-night) 中改变文本颜色
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<item name="android:actionBarStyle">@style/MyActionBarStyle</item>
</style>
<style name="MyActionBarStyle" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="android:titleTextStyle">@style/MyActionBarTitleTextStyle</item>
</style>
<style name="MyActionBarTitleTextStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但它不起作用。ActionBar 中的标题文本颜色仍然是黑色。
android android-appcompat android-actionbar android-darkmode
从 Android 10 开始,您可以在暗模式和默认亮模式之间切换。我还没有对此进行更深入的研究,因为这是一个新话题。暗模式颜色是由操作系统自动切换的,还是有什么方法可以告诉我的应用程序在暗模式打开时切换不同的应用程序主题?某些 Android 9 设备也可以使用暗模式。
因为我使用自定义参数制作了自定义深色主题,并且我为资源中的每种颜色设置了深色(使用自定义属性attrs.xml并将特定颜色资源应用于主题中的主题styles.xml)。同样它适用于我的应用程序的不同配色方案(例如蓝色、红色、绿色)。这样我就可以知道我的应用程序而不是系统中的不同视图将使用哪种颜色。
我唯一需要做的就是检测系统中是否开启/关闭暗模式。我可以强制用户在应用设置(自定义设置)中打开暗模式,但主题可能会受到系统暗模式(在手机设置中打开)的影响。
我使用 React-Naive 制作了一个应用程序
“反应本机”:“0.62.0”,
“反应”:“16.11.0”,
当我将 Android 设备置于黑暗模式时,我的 UI 设计受到干扰,因此我想暂时将其禁用。
请帮忙。
当在启用了深色主题的 Android 手机上时,我的 Xamarin.Forms 应用程序(Shell 项目)会自动切换到深色主题。我不希望这种事发生。我尝试了多种方法来禁用此功能,但没有一个起作用。知道出了什么问题吗?
AppShell.xaml 中代码中有趣的部分是:
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:retrogamez="clr-namespace:RetroGameZ"
Title="RetroGameZ"
x:Class="RetroGameZ.AppShell">
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="#049DBF" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#03A6A6" />
<Setter Property="Shell.UnselectedColor" Value="#D3D3D3" />
<Setter Property="Shell.TabBarBackgroundColor" Value="#049DBF" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#D3D3D3"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
Run Code Online (Sandbox Code Playgroud)
后来就只有单独的组件了。
android xamarin.android xamarin xamarin.forms android-darkmode
我正在使用 Theme.Material.Components.DayNight 主题在我的应用程序中实现暗模式。问题是我的暗模式将纯粹与暗成分混合,而我的“亮”模式将与混合的暗和亮成分混合。因此,当应用程序默认模式设置为 Light 时,我需要以某种方式访问深色主题属性。你能帮我解决我的问题吗?
我正在考虑设置自定义主题“DarkThemeInLight”并将其分配给应该是黑暗的视图,但这是一个好方法吗?如果我直接在该主题定义中设置颜色,我将重复 color.xml(night) 中几乎一半的代码。
我正在考虑的方法:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorSecondary">@color/colorSecondary</item>
<item name="android:colorBackground">@color/colorBackground</item>
<item name="colorSurface">@color/colorSurface</item>
<item name="colorError">@color/colorOnError</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="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="drawerArrowStyle">@style/DrawerIcon</item>
<item name="textAppearanceSubtitle1">@style/TextAppearance.MyTheme.Subtitle1</item>
<item name="textAppearanceSubtitle2">@style/TextAppearance.MyTheme.Subtitle2</item>
<item name="textAppearanceCaption">@style/TextAppearance.MyTheme.Caption</item>
<item name="android:colorControlActivated">@color/colorPrimary</item>
</style>
<style name="DarkThemeInLight" parent="AppTheme">
<item name="android:colorBackground">@night/colorsBackground</item> (Can i call night folder somehow?)
or
<item name="android:colorBackground">#121212</item> (Value from night colors folder)
...
</style>
Run Code Online (Sandbox Code Playgroud)
2020 年 6 月 4 日更新:
最终,我能够达到我提到的效果。由于两个独立的 AppTheme 和 AppTheme.Dark 样式的定义,我能够将某些组件样式化为浅色,而将其他组件样式化为深色,但我的解决方案排除了在浅色和深色模式之间切换的可能性,因此我的问题仍然存在
<style …Run Code Online (Sandbox Code Playgroud) 我正在实现黑暗模式,并且我有一个黑暗模式版本的启动画面drawable-night。然后在应用程序中onCreate我调用:
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
Run Code Online (Sandbox Code Playgroud)
不过,似乎启动画面仍然遵循系统设置,而不是我设置的。我尝试在应用程序中执行相同的操作init,但产生了相同的结果。有没有办法做到这一点?
我需要知道用户是否在操作系统级别启用了深色主题,以便我可以在我的应用程序中自动启用深色主题。(不强制深色模式)
到目前为止,我从谷歌了解到,你必须使用Theme.Appcompat.Daynight或 Material Daynight 主题来使你的应用程序支持黑暗模式。并且您需要在values和values-night目录中使用不同的styles.xml。我不愿意让我的应用程序支持深色主题。但是当我将 Android 系统主题(从通知面板)更改为深色时,我的应用程序变黑。我使用Theme.Appcompat.Light作为基本主题,没有为我的应用程序支持黑暗模式做任何事情,但我的应用程序仍然变暗。我是第一次使用cardview,不知道这是否是原因,因为我对android编程很陌生。我们非常欢迎您的一点帮助。请在此处检查屏幕截图:
android android-theme android-cardview android-dark-theme android-darkmode
我正在尝试实现夜间模式,但我遇到了问题:CardView不会将颜色更改为night值,但活动正确地将其背景更改为夜间颜色值。

请帮助理解这个问题。
MainActivity
public class NewFinishActivity extends AppCompatActivity {
@BindView(R.id.finishRecycler)
RecyclerView finishRecycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
setContentView(R.layout.activity_new_finish);
ButterKnife.bind(this);
LevelRecyclerAdapter mAdapter = new LevelRecyclerAdapter(getApplicationContext(), new ArrayList<>());
finishRecycler.setAdapter(mAdapter);
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity 布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".activities.NewFinishActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/finishRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
适配器
public class LevelRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<UserLevel> mItems;
private Context mContext;
public LevelRecyclerAdapter(Context context, List<UserLevel> items) {
mContext = context; …Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-recyclerview android-darkmode
我在手动管理暗模式时遇到了一个大问题。
在应用程序启动时,第一个活动会在 SharedPreferences 中检查用户是否选择启用深色模式或保持浅色模式(默认),然后根据用户首选项进行相应更改:
编辑:正如 CSmith 所要求的,这是在活动中调用主题选择器的时间
//FirstActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppThemeSelector.checkAndShowTheme(getApplicationContext());
...
Run Code Online (Sandbox Code Playgroud)
这是主题选择器方法
//AppThemeSelector.class
public static void checkAndShowTheme(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences(
PreferencesValues.SHARED_DARKMODE_MAP, Context.MODE_PRIVATE
);
boolean darkMode = sharedPreferences.getBoolean(PreferencesValues.SHARED_DARKMODE_ENABLED, false);
if(darkMode){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,每当它启动时,它都会使用用户选择的主题设置主题,并且一切正常。
显然,这个问题似乎只发生在我的小米 Mi A2 Lite (Android 10) 上,因为我已经在 Pixel 2 (Android 11) 和模拟器 (Android 10) 上进行了测试,当应用程序从最近的应用程序恢复时,一切正常普通的。
重现我的问题的一致方法是:假设我的应用程序设置为手动灯光模式,而我的系统处于黑暗模式。我打开应用程序(留在家庭活动或去其他活动会导致相同的问题),然后进入系统设置并更改系统语言。
当我从最近的应用程序选项卡重新打开应用程序时,应用程序似乎尝试重新启动,但中途崩溃了(不确定这一点,因为 logcat 没有显示任何有关崩溃的信息,应用程序也没有显示有关崩溃的任何对话框),并且当它启动时,它会自动设置它的主题到系统主题(所以在我的示例中它进入黑暗模式)。
目前的问题是,一些本来应该在深色模式下看到的文本现在有了浅色模式主题,使它们与黑色背景融为一体;应用程序中用于从深色模式更改为浅色模式的开关保持正确的设置(显示未启用深色模式),但应用程序处于深色模式。如果应用程序重新启动,一切都会恢复正常,并且应用程序会正确显示在浅色主题中。
当应用程序长时间停留在后台时,我无法重现该问题,但我认为它也可能发生。
android-darkmode ×10
android ×9
android-10.0 ×1
android-view ×1
kotlin ×1
react-native ×1
styles ×1
themes ×1
xamarin ×1