Android Wear DayNight主题应用程序

Android Wear DayNight主题应用程序,android,wear-os,android-appcompat,android-theme,Android,Wear Os,Android Appcompat,Android Theme,我试图在我的Android Wear应用程序上使用AppCompat DayNight主题,但它不起作用,我的活动需要环境模式,因此我扩展了WearableActivity,如下所示: public class BaseActivity extends WearableActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate

我试图在我的Android Wear应用程序上使用AppCompat DayNight主题,但它不起作用,我的活动需要环境模式,因此我扩展了WearableActivity,如下所示:

public class BaseActivity extends WearableActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAmbientEnabled();
        ....
    }

}
我的主题是:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/colorBackground</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/textColorPrimary</item>
    </style>

真的
@颜色/彩色背景
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/文本颜色主
但什么都不起作用,主题根本没有改变。。。我在我的移动应用程序中使用相同的主题,并且它可以工作,唯一的区别是我的活动扩展了AppCompatActivity


有没有办法让它适用于Android Wear应用程序

我通过在我的WearableActivity(从AppCompativeActivity复制/粘贴)中添加此项,设法使其适用于我的用例(强制白天或晚上):


现在我可以使用
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE\u NIGHT\u YES/NO)

我通过在我的WearableActivity(从AppCompativeActivity复制/粘贴)中添加此代码,使其适用于我的用例(强制白天或晚上):


现在我可以使用
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE\u NIGHT\u YES/NO)

您是否也可以分享在可穿戴设备上运行应用程序时发生的情况?控制台中出现任何错误?或者,您可能想尝试使用本文中提到的
AppCompatDelegate.setDefaultNightMode()
,看看它是否适合您。要了解更多信息,您可能还想访问。当我运行设备时,没有附加任何内容。这只是“白天”主题即使我使用setDefaultNightMode强制夜间主题,我也会查看AppCompatActivity的源代码,其中有一些代码可以在需要时应用正确的主题,我将尝试将该代码复制/粘贴到可穿戴活动中,看看它是否有效。您还可以分享在可穿戴设备上运行应用程序时发生的情况吗?控制台中出现任何错误?或者,您可能想尝试使用本文中提到的
AppCompatDelegate.setDefaultNightMode()
,看看它是否适合您。要了解更多信息,您可能还想访问。当我运行设备时,没有附加任何内容。这只是“白天”主题即使我使用setDefaultNightMode强制夜间主题,我也会查看AppCompatActivity的源代码,其中有一些代码可以在需要时应用正确的主题,我将尝试将该代码复制/粘贴到可穿戴活动中,看看它是否有效
public class BaseActivity extends WearableActivity implements AppCompatCallback {
    private AppCompatDelegate delegate;
    private int themeId = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        final AppCompatDelegate delegate = getDelegate();
        delegate.installViewFactory();
        delegate.onCreate(savedInstanceState);
        if (delegate.applyDayNight() && themeId != 0) {
            // If DayNight has been applied, we need to re-apply the theme for
            // the changes to take effect. On API 23+, we should bypass
            // setTheme(), which will no-op if the theme ID is identical to the
            // current theme ID.
            if (Build.VERSION.SDK_INT >= 23) {
                onApplyThemeResource(getTheme(), themeId, false);
            } else {
                setTheme(themeId);
            }
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setTheme(@StyleRes final int resid) {
        super.setTheme(resid);
        // Keep hold of the theme id so that we can re-set it later if needed
        themeId = resid;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getDelegate().onSaveInstanceState(outState);
    }

    /**
     * @return The {@link AppCompatDelegate} being used by this Activity.
     */
    @NonNull
    public AppCompatDelegate getDelegate() {
        if (delegate == null) {
            delegate = AppCompatDelegate.create(this, this);
        }
        return delegate;
    }

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {

    }

    @Override
    public void onSupportActionModeFinished(ActionMode mode) {

    }

    @Nullable
    @Override
    public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
        return null;
    }
}