三、样式 & 夜间模式切换
效果图:

一、主题样式
1、values/theme_attr.xml 自定义主题属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="lineColor" format="reference" /><!-- 分割线颜色 -->
<attr name="layout_bg_normal" format="reference"/>
<attr name="layout_item_bg" format="reference"/>
</resources>
2、values/theme_color.xml 主题相关颜色单独一个文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- day -->
<color name="day_colorPrimary">#40AA53</color>
<color name="day_colorPrimaryDark">#0b751e</color>
<color name="day_line_color">#e7e7e7</color>
<color name="day_layout_bg_normal">#f6f6f6</color>
<color name="day_layout_bg_pressed">#dfdfdf</color>
<!-- night -->
<color name="night_colorPrimary">#008000</color>
<color name="night_colorPrimaryDark">#0b751e</color>
<color name="night_line_color">#282828</color>
<color name="night_layout_bg_normal">#222222</color>
<color name="night_layout_bg_pressed">#0d0e11</color>
</resources>
3、values/theme.xml 日间/夜间主题样式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DayTheme" parent="@style/Theme.AppCompat.Light">
<item name="colorPrimary">@color/day_colorPrimary</item> <!-- ActionBar Background Color -->
<item name="colorPrimaryDark">@color/day_colorPrimaryDark</item> <!-- 状态栏颜色 -->
<item name="android:textColorLink">#0b9a27</item>
<item name="colorAccent">@color/day_colorPrimary</item>
<item name="android:textColorPrimary">@color/white</item> <!-- ActionBar Title Color -->
<item name="lineColor">@color/day_line_color</item>
<item name="layout_bg_normal">@color/day_layout_bg_normal</item>
<item name="layout_item_bg">@drawable/day_list_item_backgound</item>
</style>
<style name="NightTheme" parent="@style/Theme.AppCompat">
<item name="colorPrimary">@color/night_colorPrimary</item> <!-- ActionBar Background Color -->
<item name="colorPrimaryDark">@color/night_colorPrimaryDark</item> <!-- 状态栏颜色 -->
<item name="android:textColorLink">#0b9a27</item>
<item name="colorAccent">@color/night_colorPrimary</item>
<item name="android:textColorPrimary">@color/white</item> <!-- ActionBar Title Color -->
<item name="lineColor">@color/night_line_color</item>
<item name="layout_bg_normal">@color/night_layout_bg_normal</item>
<item name="layout_item_bg">@drawable/night_list_item_backgound</item>
</style>
</resources>
4、可使用 selector选择器 控制背景变化:drawable/day_list_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/day_layout_bg_pressed" />
<item android:state_focused="true" android:drawable="@color/day_layout_bg_pressed" />
<item android:state_selected="true" android:drawable="@color/day_layout_bg_pressed" />
<item android:state_pressed="true" android:drawable="@color/day_layout_bg_pressed" />
<item android:drawable="@color/day_layout_bg_normal" />
</selector>
5、dp & px
小米4c尺寸:5英寸,1920 x 1080 px ;
dpi = 1920^2+1080^2 开方 = 440
density = 440/160 =2.75
dp 模式下长宽为 698 x 392 , 基于 px=dp*density
二、夜间主题切换
1、 主题模式由SharePreference字段保存,Activity.onCreate中设置主题,该部分代码需要写在 super.onCreate()之前;
主题切换处理逻辑只需更改SharePreference字段后,将Activity.recreate();
private static final String KEY_NIGHT_MODE_SWITCH="key_night_mode_switch";
//夜间模式
public static boolean getNightModeSwitch(){
return getPreference(KEY_NIGHT_MODE_SWITCH,false);
}
//设置夜间模式
public static void setNightModeSwitch(boolean on){
setPreference(KEY_NIGHT_MODE_SWITCH,on);
}
public void onCreate(Bundle savedInstanceState) {
if(AppContext.getNightModeSwitch()){
setTheme(R.style.NightTheme);
}else{
setTheme(R.style.DayTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
}
public void onClick(View v) {
switch (v.getId()){
case R.id.drawer_menu_item_theme:
AppContext.setNightModeSwitch(!AppContext.getNightModeSwitch());
getActivity().recreate();
break;
}
}
参考文章:
鸿洋-Android 5.x Theme 与 ToolBar 实战 http://blog.csdn.net/lmj623565791/article/details/45303349
浙公网安备 33010602011771号