Android Wear DayNight Theme AppCompat
Android Wear DayNight Theme AppCompat
我正在尝试在我的 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>
但没有任何效果,主题根本没有改变...我在我的移动应用程序中使用相同的主题并且它有效,唯一的区别是我的 activity extend AppCompatActivity .
有没有办法让它适用于 Android Wear 应用程序?
我通过将此添加到我的 WearableActivity(copy/paste 来自 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);
我正在尝试在我的 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>
但没有任何效果,主题根本没有改变...我在我的移动应用程序中使用相同的主题并且它有效,唯一的区别是我的 activity extend AppCompatActivity .
有没有办法让它适用于 Android Wear 应用程序?
我通过将此添加到我的 WearableActivity(copy/paste 来自 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);