首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MODE_NIGHT_NO之后的MODE_NIGHT_FOLLOW_SYSTEM

MODE_NIGHT_NO之后的MODE_NIGHT_FOLLOW_SYSTEM
EN

Stack Overflow用户
提问于 2019-03-09 16:04:25
回答 1查看 1.5K关注 0票数 2

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

应用程序在以下系统模式下启动:应用程序样式较轻。

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

我正在切换回系统模式:应用程序风格是黑色的。

我正在切换到日间模式:应用程序的风格是轻量级的。

我切换回系统模式:应用程序风格是轻量级的。

SdkVersion: 28,在Android4.0.3(模拟器)和6.0.1 (真实设备)上测试。

我的代码:

代码语言:javascript
运行
复制
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);

活动:

代码语言:javascript
运行
复制
<?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>

和样式:

代码语言:javascript
运行
复制
<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>

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-05-10 23:41:11

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55075267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档