android夜间模式切换
Posted gali
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android夜间模式切换相关的知识,希望对你有一定的参考价值。
1.记录个最简单的夜间模式的实现
2.styles.xml
<style name="DayTheme" parent="AppTheme"> <item name="color_title">@color/title_bai</item> <item name="color_background">@drawable/bg01</item> </style> <style name="NightTheme" parent="AppTheme"> <item name="color_title">@color/title_ye</item> <item name="color_background">@drawable/bg02</item> </style> <style name="FragmentTheme1" parent="AppTheme"> <item name="fragmentcolor_title">@color/fragment_textcolor1</item> </style>
3.colors.xml
<color name="title_bai">#000000</color> <color name="title_ye">#ffffff</color> <color name="background_bai">#ffffff</color> <color name="background_ye">#8e7d7d</color> <color name="fragment_textcolor1">#D43333</color> <color name="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"?> <LinearLayout xmlns: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"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textColor="?attr/color_title" android:text="fragment1"/> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_weight="1" android:textColor="?attr/color_title" android:layout_height="match_parent" android:text="fragment2"/> <Button android:id="@+id/btn3" android:layout_width="0dp" android:textColor="?attr/color_title" android:layout_weight="1" android:layout_height="match_parent" android:text="activity2"/> <Button android:id="@+id/btn4" android:layout_width="0dp" android:textColor="?attr/color_title" android:layout_weight="1" android:layout_height="match_parent" android:text="切换"/> </LinearLayout> <FrameLayout android: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 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Context ctxWithTheme = new ContextThemeWrapper(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); return rootView; }
8.链接:
http://pan.baidu.com/s/1eSiU42Q
以上是关于android夜间模式切换的主要内容,如果未能解决你的问题,请参考以下文章