Android夜间模式的几种实现
一、直接修改widget顏色,這種方式實現(xiàn)起來最簡單,但需要每個控件都去修改,太過復(fù)雜。例如:
/*** 相應(yīng)交互,修改控件顏色* @param view*/public void onMethod1Click(View view) {if (view.getId() == R.id.btn_method1) {int theme = NightModeUtils.getSwitchDayNightMode(this);NightModeUtils.setBackGroundColor(this, mRootView, theme);NightModeUtils.setTextColor(this, findViewById(R.id.text), theme);NightModeUtils.setDayNightMode(this, theme);}}NightModeUitls修改顏色方法
/*** 修改背景色* @param context* @param view* @param theme*/public static void setBackGroundColor(Context context, View view, int theme) {int color = context.getResources().getColor(theme == THEME_SUN ? R.color.light_color : R.color.night_color);view.setBackgroundColor(color);}/*** 修改文字色* @param context* @param view* @param theme*/public static void setTextColor(Context context, View view, int theme) {int color = context.getResources().getColor(theme == THEME_SUN ? R.color.night_color : R.color.light_color);TextView textView = (TextView)view;textView.setTextColor(color);}二、通過修改Theme,更新應(yīng)用主題。這種方法問題在于,需要重啟Activity才能完成界面渲染。
在Activity中調(diào)用setContentView之前進行Theme設(shè)置:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);NightModeUtils.onActivityCreateSetTheme(this);setContentView(R.layout.activity_main);}?
NightModeUitls設(shè)置Theme方法:
/** Set the theme of the activity, according to the configuration. */public static void onActivityCreateSetTheme(Activity activity) {int theme = getDayNightMode(activity);switch (theme) {case THEME_SUN:activity.setTheme(R.style.AppSunTheme);break;case THEME_NIGHT:activity.setTheme(R.style.AppNightTheme);break;default:break;}}?
三、通過怎加一層遮光罩來實現(xiàn)。效果不是很理想。
通過WindowManager,將一個透明背景的TextView加到Activity主界面中。代碼如下:
private void night() {if (mNightView == null) {mNightView = new TextView(this);mNightView.setBackgroundColor(0xaa000000);}WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.TYPE_APPLICATION,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);lp.gravity = Gravity.BOTTOM;lp.y = 10;try {mWindowManager.addView(mNightView, lp);} catch (Exception ex) {}}private void day() {try {mWindowManager.removeView(mNightView);} catch (Exception ex) {}}四、最后來看一下Dialog需要怎么實現(xiàn)夜間模式。
AlertDialog.Builder 有一個帶style id參數(shù)的構(gòu)造函數(shù),我們就通過這個構(gòu)造函數(shù)來實現(xiàn)Dialog主題的修改,從而達到夜間模式。
public static AlertDialog.Builder createBuilder(Context context) {if (NightModeUtils.getDayNightMode(context) == NightModeUtils.THEME_SUN) {return new AlertDialog.Builder(context);} else {return new AlertDialog.Builder(context, R.style.NightDialog);}}我們通過如上方法來獲取Builder,實現(xiàn)主題切換。其中R.style.NightDialog我采用如下方式:
<style name="NightDialog" parent="android:Theme.Holo.Dialog"><item name="android:windowBackground">@android:color/transparent</item></style>在android?honeycomb之前的版本Theme.Dialog.Alert與AlertDialog這兩個style是public的,可以通過修改主題時,重新定義這兩個style實現(xiàn)dialog主題的修改,但之后的版本已經(jīng)將他們開放關(guān)閉了。所以,我通過上面的辦法實現(xiàn)了dialog的主題修改。
Demo源碼
來源:?http://www.cnblogs.com/yuanzhanxue/p/3470820.html
來自為知筆記(Wiz)
總結(jié)
以上是生活随笔為你收集整理的Android夜间模式的几种实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Spring Boot官方文档》16.
- 下一篇: 针对各组项目的改进意见