1. Record the simplest implementation of night mode
2.styles.xml
<stylename= "Daytheme"Parent= "Apptheme"><item name= "Color_title" > @color/title_bai</item> <item name= "Color_background" > @drawa Ble/bg01</item></style> <stylename= "Nighttheme"Parent= "Apptheme"><item name= "Color_title" > @color/title_ye</item> <item name= "Color_background" > @drawa Ble/bg02</item></style> <stylename= "FragmentTheme1"Parent= "Apptheme"><item name= "Fragmentcolor_title" > @color/fragment_textcolor1</item></style>
3.colors.xml
<Colorname= "Title_bai">#000000</Color> <Colorname= "Title_ye">#ffffff</Color> <Colorname= "Background_bai">#ffffff</Color> <Colorname= "Background_ye">#8e7d7d</Color> <Colorname= "Fragment_textcolor1">#D43333</Color> <Colorname= "Fragment_textcolor2">#71C41F</Color>
4.arrts.xml
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "color_title" format= "Color"/> <attr name= "color_background" format= "color"/> <attr name= "fragmentcolor_title" format= "Color" /></resources>
5.main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background = "? Attr/color_background"Tools:context= "Com.example.guoxw.myapplication.MainActivity"Android:weightsum= "1"> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "40DP"android:orientation= "Horizontal"> <ButtonAndroid:id= "@+id/btn1"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Match_parent"Android:textcolor= "? Attr/color_title"Android:text= "Fragment1"/> <ButtonAndroid:id= "@+id/btn2"Android:layout_width= "0DP"Android:layout_weight= "1"Android:textcolor= "? Attr/color_title"Android:layout_height= "Match_parent"Android:text= "Fragment2"/> <ButtonAndroid:id= "@+id/btn3"Android:layout_width= "0DP" android:textcolor= "? Attr/color_title" Android:layout_weight= "1"Android:layout_height= "Match_parent"Android:text= "Activity2"/> <ButtonAndroid:id= "@+id/btn4"Android:layout_width= "0DP"Android:textcolor= "? Attr/color_title"Android:layout_weight= "1"Android:layout_height= "Match_parent"Android:text= "Toggle"/> </LinearLayout> <FramelayoutAndroid:id= "@+id/fragmentlayout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_marginbottom= "? Actionbarsize"Android:layout_margintop= "? Actionbarsize" /> </LinearLayout>
6.java-activity
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); if (constant.isday) { this.settheme (r.style.daytheme); } else { this.settheme (r.style.nighttheme); } Setcontentview (R.layout.activity_main);
}
7.java--fragment
@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestat e) {Context ctxwiththeme=NewContextthemewrapper (Getactivity (). Getapplicationcontext (), r.style.fragmenttheme1); Layoutinflater Locallayoutinflater=Inflater.cloneincontext (Ctxwiththeme); ViewGroup Rootview= (ViewGroup) locallayoutinflater.inflate (R.layout.fragment_fragment1,NULL,false); //view=inflater.inflate (R.layout.fragment_fragment1, container, false); returnRootview; }
8. Links:
http://pan.baidu.com/s/1esiu42q
Android Night mode switch