<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="textColor" format="color|reference" /> <attr name="mainBackground" format="color|reference" /> </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 默认 -->
<style name="ThemeDefault" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="mainBackground">#ffffff</item>
<item name="textColor">#000000</item>
</style>
<!-- 夜间 -->
<style name="ThemeNight" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="mainBackground">#000000</item>
<item name="textColor">#ffffff</item>
</style>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android="@+id/main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?attr/mainBackground">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="改变Theme"
android:onClick="changeTheme"
android:textColor="?attr/textColor"/>
</LinearLayout>
public void changeTheme() {
if (isNightMode) {
setTheme(R.style.ThemeDefault);
isNightMode = false;
} else {
setTheme(R.style.ThemeNight);
isNightMode = true;
}
setContentView(R.layout.activity_main);
}
private void updateTheme() {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getTheme();
theme.resolveAttribute(R.attr.textColor, typedValue, true);
findViewById(R.id.button).setBackgroundColor(typedValue.data);
theme.resolveAttribute(R.attr.mainBackground, typedValue, true);
findViewById(R.id.main_screen).setBackgroundColor(typedValue.data);
}
public static Drawable getDrawable(Context context, int id) {
return context.getResources().getDrawable(getResId(id));
}
public static int getResId(int defaultResId) {
if (!isNightMode()) {
return defaultResId;
}
if (sResourceMap == null) {
buildResourceMap();
}
int themedResId = sResourceMap.get(defaultResId);
return themedResId == 0 ? defaultResId : themedResId;
}
private static void buildResourceMap() {
sResourceMap = new SparseIntArray();
sResourceMap.put(R.drawable.common_background, R.drawable.common_background_night);
// ...
}
public static void init(Context context) {
sRes = context.getResources();
}
public static void updateNightMode(boolean on) {
DisplayMetrics dm = sRes.getDisplayMetrics();
Configuration config = sRes.getConfiguration();
config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
config.uiMode |= on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
sRes.updateConfiguration(config, dm);
}
/**
* Sets the night mode. Changes to the night mode are only effective when
* the car or desk mode is enabled on a device.
*
* The mode can be one of:
* {@link #MODE_NIGHT_NO}- sets the device into notnight
* mode.
* {@link #MODE_NIGHT_YES} - sets the device into night mode.
* {@link #MODE_NIGHT_AUTO} - automatic night/notnight switching
* depending on the location and certain other sensors.
*/
public void setNightMode(int mode)
if (isDoingNightModeLocked() && mNightMode != mode) {
Settings.Secure.putInt(getContext().getContentResolver(), Settings.Secure.UI_NIGHT_MODE, mode);
mNightMode = mode;
updateLocked(0, 0);
}
boolean isDoingNightModeLocked() {
return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
}
/**
* Bit mask of the ui mode. Currently there are two fields:
*
The {@link #UI_MODE_TYPE_MASK} bits define the overall ui mode of the
* device. They may be one of {@link #UI_MODE_TYPE_UNDEFINED},
* {@link #UI_MODE_TYPE_NORMAL}, {@link #UI_MODE_TYPE_DESK},
* {@link #UI_MODE_TYPE_CAR}, {@link #UI_MODE_TYPE_TELEVISION},
* {@link #UI_MODE_TYPE_APPLIANCE}, or {@link #UI_MODE_TYPE_WATCH}.
*
*
The {@link #UI_MODE_NIGHT_MASK} defines whether the screen
* is in a special mode. They may be one of {@link #UI_MODE_NIGHT_UNDEFINED},
* {@link #UI_MODE_NIGHT_NO} or {@link #UI_MODE_NIGHT_YES}.
*/
public int uiMode;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有