android - AppCompat DayNight 主题不适用于 Android 6.0?

标签 android xml android-layout android-support-library android-theme

我正在使用 Android 支持库 23.2 添加的新 Theme.AppCompat.DayNight

在 Android 5.1 上运行良好。

在 Android 6.0 上,Activity 看起来像使用浅色主题,但对话框看起来像使用深色主题。

我的应用类:

public class MyApplication extends Application {
    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_YES);
    }
}

我的样式.xml

<style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="Dialog.Alert" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>

显示对话框的代码:

new AlertDialog.Builder(mContext, R.style.Dialog_Alert)
                .setTitle("Title")
                .setMessage("Message")
                .show();

最佳答案

Google 已经修复它以支持 23.2.1

旧答案:

在Android 6.0上,系统的夜间模式设置默认为UiModeManager.MODE_NIGHT_NO,它会在调用onCreate之前更改Resources.Configuration.uiMode .但是,支持库在 AppCompatActivity 中的 onCreate 中应用其夜间模式设置,为时已晚,我认为这就是它不能在 6.0 上运行的原因。

因此,如果我们可以重写 AppCompatActivity 中的 getResources() 并更改 uiMode

旧答案:

这里是修复在 Android 6.0 上不工作的代码

public class Application extends android.app.Application {
    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // add this code for 6.0
        // DO NOT DO THIS. It will trigger a system wide night mode.
        // This is the old answer. Just update appcompat.
        // UiModeManager uiManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
        // uiManager.setNightMode(UiModeManager.MODE_NIGHT_);
    }
}

注意:如果您的应用没有位置权限,您的应用将不会得到与系统相同的计算结果。这意味着支持库可能认为现在是晚上,而系统没有,这将导致您的一些 UI 看起来很暗一些亮。

最好的方法是等待 Google 修复它。

关于android - AppCompat DayNight 主题不适用于 Android 6.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665199/

相关文章:

android - 为图像圆形图像android添加边框

java - 删除Android应用程序中的通知栏阴影

ruby-on-rails - Rails to_xml 将选项添加到根节点

android - xmlns :android ="http://schemas.android.com/apk/res/android" gets added to all my layout objects causing errors

android - 如何打开android日历应用程序并跳转到指定日期?

没有网页的 Android 应用索引

java - DocumentBuilder 解析在命中 '&amp;' 时中断字符串

xml - XSD 中复杂类型的选择

android - 通过形状资源文件设置圆角的 ConstraintLayout 不起作用

android - 单击 EditText 打开 DatePickerDialog 需要两次单击