Android Wear DayNight主题AppCompat

时间:2017-04-16 16:06:23

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

我试图在我的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应用程序?

1 个答案:

答案 0 :(得分:0)

我设法通过在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);