上面是选择style中的两种模式的工具类
<RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/pad_height_50" > <TextView android:id="@+id/night_mode_tv" android:layout_width="match_parent" android:layout_height="@dimen/pad_height_50" android:layout_centerVertical="true" android:layout_marginLeft="@dimen/pad_height_10" android:layout_marginRight="@dimen/pad_height_3" android:padding="@dimen/pad_height_10" android:gravity="center_vertical" android:drawableLeft="@drawable/yejian" android:drawablePadding="@dimen/pad_height_10" android:text="夜间模式" style="@style/subtitle_text" /> <com.hipad.news.view.UISwitchButton xmlns:switch="http://schemas.android.com/apk/res-auto" android:id="@+id/night_mode_switch_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:padding="@dimen/pad_height_10" android:gravity="center_vertical" switch:bmHeight="50000dp" switch:bmWidth="@dimen/pad_height_40" /> </RelativeLayout>添加switch选择按钮布局 night_mode_switch = (UISwitchButton) view.findViewById(R.id.night_mode_switch_one); boolean checked = (Boolean) SpUtils.get(getActivity(), "night_day", true); night_mode_switch.setChecked(checked); night_mode_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { SpUtils.put(getActivity(), "night_day", true); Log.d("zhsy", "night=="); night(); } else { SpUtils.put(getActivity(), "night_day", false); Log.d("zhsy", "day=="); day(); } } });对选择按钮控件的监听 private void night() { if (mNightView == null) { mNightView = new TextView(getActivity()); 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) { } }通过WindowManager控制蒙版的添加和删除来模拟夜间和白天的模式 NightModeUtils.onActivityCreateSetTheme(getActivity());在oncreate方法中调用控制切换主题的方法
