2

切换到 MODE_NIGHT_FOLLOW_SYSTEM 模式时,将应用上次使用的模式:

应用程序以跟随系统模式启动:应用程序风格较轻。

我正在切换到夜间模式:应用程序风格很暗。

我正在切换回遵循系统模式:应用程序样式很暗。

我正在切换到白天模式:应用程序风格很轻。

我正在切换回遵循系统模式:应用程序风格很轻。

SdkVersion:28,在 Android 4.0.3(模拟器)和 6.0.1(真机)上测试。

我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView currentText = findViewById(R.id.current);
        currentText.setText("Current: " + AppCompatDelegate.getDefaultNightMode());
    }

    private void changeNightMode(int nightMode) {
        AppCompatDelegate.setDefaultNightMode(nightMode);
        recreate();
    }

    public void onSystem(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }
    public void onNo(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    public void onYes(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
    public void onAuto(View view) {
        this.changeNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);

活动:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/current"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/system"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onSystem"
        android:text="System"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/current" />

    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onNo"
        android:text="No"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/system" />

    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onYes"
        android:text="Yes"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/no" />

    <Button
        android:id="@+id/auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onAuto"
        android:text="Auto"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/yes" />

</android.support.constraint.ConstraintLayout>

和风格:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

我究竟做错了什么?

4

1 回答 1

4

这是 AppCompat 中的一个错误。他们在 1.1.0-alpha03 中将其标记为已修复:https ://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03

于 2019-05-10T15:41:10.757 回答