下面列出了android.content.res.Configuration#UI_MODE_NIGHT_NO 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String getColorScheme(Configuration config) {
String colorScheme = "no-preference";
// Only Android 10+ support dark mode
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
colorScheme = "light";
break;
case Configuration.UI_MODE_NIGHT_YES:
colorScheme = "dark";
break;
}
}
return colorScheme;
}
@Override
protected void onResume() {
super.onResume();
int nightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightMode) {
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
if (LinphonePreferences.instance().isDarkModeEnabled()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
case Configuration.UI_MODE_NIGHT_YES:
if (!LinphonePreferences.instance().isDarkModeEnabled()) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case Configuration.UI_MODE_NIGHT_YES:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
}
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
}
private static String getCurrentTheme(Context context) {
String defaultTheme = context.getString(R.string.tb_pref_theme_default);
SharedPreferences pref = getSharedPreferences(context);
String themePref = pref.getString(PREF_THEME, defaultTheme);
if(themePref.equals("system")) {
Configuration configuration = context.getResources().getConfiguration();
int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch(currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
return "light";
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
return "dark";
}
} else
return themePref;
return defaultTheme;
}
@Override
public void onClick(View view) {
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO: {
setDayNightMode(AppCompatDelegate.MODE_NIGHT_YES);
// Night mode is not active, we're in day time
break;
}
case Configuration.UI_MODE_NIGHT_YES:{
setDayNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Night mode is active, we're at night!
break;
}
case Configuration.UI_MODE_NIGHT_UNDEFINED: {
// We don't know what mode we're in, assume notnight
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() != 0x1
&& item.getItemId() != 0x2) {
setText(item.getItemId());
return true;
} else if (item.getItemId() == 0x1) {
getResources().getConfiguration().uiMode |= Configuration.UI_MODE_NIGHT_YES;
getResources().getConfiguration().uiMode &= ~Configuration.UI_MODE_NIGHT_NO;
getResources().updateConfiguration(getResources().getConfiguration(), getResources().getDisplayMetrics());
recreate();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
public static boolean isNightModeOn(@NonNull Context context, boolean fallback) {
int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (mode) {
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_NO:
return false;
default:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
return fallback;
}
}
/**
* Check if night mode is on by system
* @param context
* @return is on / not
*/
public static boolean isNightModeOn(Context context) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
return false;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
return true;
default:
return false;
}
}
public static Resources updateNightMode(Resources resource, boolean on) {
DisplayMetrics dm = resource.getDisplayMetrics();
Configuration config = resource.getConfiguration();
final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
config.uiMode |= uiModeNightMaskNew;
resource.updateConfiguration(config, dm);
}
return resource;
}
private void ChangeThemeMode() {
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(mode == Configuration.UI_MODE_NIGHT_YES) {
RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_NO));
} else if(mode == Configuration.UI_MODE_NIGHT_NO) {
RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_YES));
}else {
//否则保持亮色主题
RxBus.getDefault().post(new ThemeModeEvent(AppCompatDelegate.MODE_NIGHT_NO));
}
}
@Override
protected void setPreferredTheme(Theme theme) {
if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
// set the theme based on the system theme
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
theme = Theme.LIGHT;
break;
case Configuration.UI_MODE_NIGHT_YES:
theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK;
break;
}
}
switch (theme) {
case LIGHT:
setTheme(R.style.AppTheme_Light_NoActionBar);
break;
case DARK:
setTheme(R.style.AppTheme_Dark_NoActionBar);
break;
case AMOLED:
setTheme(R.style.AppTheme_TrueBlack_NoActionBar);
break;
}
}
protected void setPreferredTheme(Theme theme) {
if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
// set the theme based on the system theme
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
theme = Theme.LIGHT;
break;
case Configuration.UI_MODE_NIGHT_YES:
theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK;
break;
}
}
_currentTheme = theme;
switch (theme) {
case LIGHT:
setTheme(R.style.AppTheme);
break;
case DARK:
setTheme(R.style.AppTheme_Dark);
break;
case AMOLED:
setTheme(R.style.AppTheme_TrueBlack);
break;
}
}
@Theme
@Override
public int getCurrentTheme() {
int currentNightMode = mContext.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return Theme.LIGHT;
case Configuration.UI_MODE_NIGHT_YES:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
default:
return Theme.DARK;
}
}
public static Resources updateNightMode(Resources resource, boolean on) {
DisplayMetrics dm = resource.getDisplayMetrics();
Configuration config = resource.getConfiguration();
final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
config.uiMode |= uiModeNightMaskNew;
resource.updateConfiguration(config, dm);
}
return resource;
}
@Override
protected void onResume() {
super.onResume();
int uiMode = getResources().getConfiguration().uiMode;
int dayNightUiMode = uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (dayNightUiMode == Configuration.UI_MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (dayNightUiMode == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}
private static boolean nightMode(Context context) {
int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
return true;
case Configuration.UI_MODE_NIGHT_NO:
return false;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
return false;
default:
return false;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_tracklist, container, false);
TVTracklistEmpty = view.findViewById(R.id.id_textView_TracklistEmpty);
recyclerView = view.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.getItemAnimator().setChangeDuration(0);
adapter = new TrackAdapter(data);
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
adapter.isLightTheme = true;
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
adapter.isLightTheme = false;
break;
}
recyclerView.setAdapter(adapter);
return view;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences("setting", MODE_PRIVATE);
editor = sharedPreferences.edit();
//Q导航栏沉浸
rootview = findViewById(android.R.id.content);
/*ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
v.setPadding(0,0,0,insets.getSystemWindowInsetBottom());
return insets;
}
});*/
//状态栏icon黑色
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (mode == Configuration.UI_MODE_NIGHT_NO) {
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
rootview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
ViewCompat.setOnApplyWindowInsetsListener(rootview, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
rootview.setPadding(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), 0);
return insets;
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//rootView.setFitsSystemWindows(true);
//rootView.setPadding(0,0,0,getNavigationBarHeight());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
//rootView.setFitsSystemWindows(true);
/*if ((Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)|(getNavigationBarHeight()>dp2px(16))) {
//rootView.setPadding(0,0,0,getNavigationBarHeight());
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
//rootView.setFitsSystemWindows(true);
rootView.setPadding(0,0,0,getNavigationBarHeight());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}else {
//rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//rootView.setFitsSystemWindows(true);
}
}else {
rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}*/
//设置为miui主题
//setMiuiTheme(BaseActivity.this,0,mode);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//全局自定义字体
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath(sharedPreferences.getString("font_path", null))
.setFontAttrId(R.attr.fontPath)
.build()))
.build());
}
private void loadImage() {
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
case Configuration.UI_MODE_NIGHT_UNDEFINED:
Glide.with(mContext)
.load(GlideUtil.getSquare(illust))
.apply(bitmapTransform(new BlurTransformation(25, 3)))
.transition(withCrossFade())
.into(baseBind.bgImage);
break;
case Configuration.UI_MODE_NIGHT_YES:
baseBind.bgImage.setImageResource(R.color.black);
break;
}
mDetailAdapter = new IllustDetailAdapter(illust, mActivity);
mDetailAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View v, int position, int viewType) {
if (viewType == 0) {
Intent intent = new Intent(mContext, ImageDetailActivity.class);
intent.putExtra("illust", illust);
intent.putExtra("dataType", "二级详情");
intent.putExtra("index", position);
if (Shaft.sSettings.isFirstImageSize()) {
mActivity.startActivity(intent);
} else {
if (mDetailAdapter.getHasLoad().get(position)) {
Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity,
v, "big_image_" + position).toBundle();
startActivity(intent, bundle);
} else {
mActivity.startActivity(intent);
}
}
} else if (viewType == 1) {
}
}
});
baseBind.recyclerView.setAdapter(mDetailAdapter);
}
@Override
public void onMapReady(GoogleMap map) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
map.setMapType(preferences.getInt(getString(R.string.baseLayerKey), getResources().getInteger(R.integer.baseLayerDefaultValue)));
int dayNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (dayNightMode == Configuration.UI_MODE_NIGHT_NO) {
map.setMapStyle(null);
} else {
map.setMapStyle(MapStyleOptions.loadRawResourceStyle(getApplicationContext(), R.raw.map_theme_night));
}
if (latLng != null && icon != null) {
map.addMarker(new MarkerOptions()
.position(latLng)
.icon(icon));
LocationProperty accuracyProperty = location.getPropertiesMap().get("accuracy");
if (accuracyProperty != null) {
float accuracy = Float.parseFloat(accuracyProperty.getValue().toString());
int color = LocationBitmapFactory.locationColor(getApplicationContext(), location);
map.addCircle(new CircleOptions()
.center(latLng)
.radius(accuracy)
.fillColor(ColorUtils.setAlphaComponent(color, (int) (256 * .20)))
.strokeColor(ColorUtils.setAlphaComponent(color, (int) (256 * .87)))
.strokeWidth(2.0f));
double latitudePadding = (accuracy / 111325);
LatLngBounds bounds = new LatLngBounds(
new LatLng(latLng.latitude - latitudePadding, latLng.longitude),
new LatLng(latLng.latitude + latitudePadding, latLng.longitude));
int minDimension = Math.min(mapView.getWidth(), mapView.getHeight());
int padding = (int) Math.floor(minDimension / 5f);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
} else {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
}
}
}