Android 、切换主题的实现
2015-07-27 15:02
597 查看
通过重启Activity,调用setTheme()方法设置相应的Theme来实现的。
style.xml定义
在layout中使用主题
layout/main.xml
public void setTheme (int resid)
Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).
https://github.com/dersoncheng/MultipleTheme
1. 定义两套主题
attrs.xml定义<resources> <attr name="main_bg" format="reference|color"/> <attr name="main_textcolor" format="reference|color"/> <attr name="second_bg" format="reference|color"/> <attr name="second_textcolor" format="reference|color"/> </resources>
style.xml定义
<style name="theme_1" > <item name="main_bg">@color/bg_main_normal</item> <item name="main_textcolor">@color/textcolor_main_normal</item> <item name="second_bg">@color/bg_second_normal</item> <item name="second_textcolor">@color/textcolor_second_normal</item> </style> <style name="theme_2"> <item name="main_bg">@color/bg_main_dark</item> <item name="main_textcolor">@color/textcolor_main_dark</item> <item name="second_bg">@color/bg_second_dark</item> <item name="second_textcolor">@color/textcolor_second_dark</item> </style> </resources>
在layout中使用主题
layout/main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/main_bg" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <TextView android:textColor="?attr/main_textcolor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn" android:text="换肤" android:layout_centerInParent="true" android:textColor="?attr/main_textcolor" android:layout_width="100dip" android:layout_height="80dip" /> <Button android:id="@+id/btn_2" android:layout_centerHorizontal="true" android:text="下一页" android:layout_below="@id/btn" android:layout_marginTop="30dip" android:textColor="?attr/main_textcolor" android:layout_width="100dip" android:layout_height="80dip" /> </RelativeLayout>
2. 设置主题
Activity的设置主题的方式public void setTheme (int resid)
Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(SharedPreferencesMgr.getInt("theme", 0) == 1) { setTheme(R.style.theme_2); } else { setTheme(R.style.theme_1); } ..... setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(SharedPreferencesMgr.getInt("theme", 0) == 1) { SharedPreferencesMgr.setInt("theme", 0); setTheme(R.style.theme_1); } else { SharedPreferencesMgr.setInt("theme", 1); setTheme(R.style.theme_2); } changeTheme(); } }); ..... }
3. 重启Activity
1) api>=11 ``` public void recreate () Added in API level 11 Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it. ``` 2) api<11 finish(); startActivity(getIntent());
public void changeTheme(){ if(Build.VERSION.SDK_INT < 11) { finish(); startActivity(getIntent()); }else{ recreate(); } }
4.其他实现方式
还有一种切换主题方式,虽然效果稍好一点(主题无缝切换),因为比较繁琐,个人不太喜欢.以上部分代码来自这个项目,喜欢的可以自行查看.https://github.com/dersoncheng/MultipleTheme
相关文章推荐
- Android旋转动画
- Android的权限机制之—— “沙箱”机制sharedUserId跟签名
- android setClickable 不起作用 没有作用
- Android旋转动画
- Android-Toolbar 使用
- 推荐android studio一个插件,可以将布局分组的
- Android,机器狗应用
- android_framework_permissions
- Android开发之动态创建布局文件及控件
- AndroidViewAnimations开源框架实现
- Android比较好的10个开源框架
- Android百度地图的简单实现
- android 实现悬架控制
- android:onClick vs setOnClickListener
- Android KITKAT 以上实现沉浸式状态栏
- Android应用性能调试
- Android电池驱动电池服务(自用)
- android intentService(三)
- Android中Bitmap格式压缩存储
- Android缩放动画