首页 > 代码库 > android夜间模式切换
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夜间模式切换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。