【问题标题】:Android Wear DayNight Theme AppCompatAndroid Wear DayNight Theme AppCompat
【发布时间】:2017-04-18 01:23:50
【问题描述】:

我正在尝试在我的 Android Wear 应用程序上使用 AppCompat DayNight 主题,但它不起作用,我的 Activity 需要环境模式,所以我像这样扩展 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 应用程序?

【问题讨论】:

  • 您能否分享一下您的应用在可穿戴设备上运行时发生的情况?控制台中出现任何错误?或者,您可能想尝试使用AppCompatDelegate.setDefaultNightMode() 中提到的tutorial,看看它是否适合您。如需更多见解,您可能还需要访问this blog
  • 运行设备时没有附加任何内容,即使我使用 setDefaultNightMode 强制夜间模式,它也只是“白天”主题,我查看了 AppCompatActivity 的源代码,其中有一些代码可以应用如果需要正确的主题,我会尝试将该代码复制/粘贴到 WearableActivity 中,看看它是否有效

标签: android wear-os android-appcompat android-theme


【解决方案1】:

通过在我的 WearableActivity 上添加它(从 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;
    }
}

现在我可以使用AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES/NO);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多