所谓的主题切换,就是能够根据不同的设定,呈现不同风格的界面给用户,也就是所谓的换肤。目前很多app都具有换肤功能,可以根据用户自己的喜好定制自己的界面,比如新浪微博,网易新闻等等。
1.1定义属性
要想根据主题的不同,设置不同属性,我们至少需要定义下属性的名字吧。要不然系统怎么知道去哪找啊!定义属性,是在values下进行的。本例中,我在attrs.xml里定义了几种属性。
attr里可以定义各种属性类型,如color、float、integer、boolean、dimension(sp、dp/dip、px、pt...)、reference(指向本地资源)等等。
可以参考http://blog.csdn.net/qq_27305737/article/details/52002858
1.2定义主题
接着,我们需要在资源文件中定义若干套主题。并且在主题中设置各个属性的值。本例中,我在styles.xml里定义了greenTheme与blueTheme。
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="greenTheme" parent="AppTheme"> <item name="textColor">#000000</item> <item name="textColor_loginlist">#000000</item> <item name="textColor_login">#ffffff</item> <item name="textColor_settings">#2f4f4f</item> <item name="textColor_settings_sub">#a9a9a9</item> <item name="actionBarBackgroundColor">#00bf00</item> <item name="cardview_bgColor">#fffff0</item> <item name="headviewColor">#56abe4</item> <item name="homepageColor">#F0F0F0</item> </style> <style name="blueTheme" parent="AppTheme"> <item name="textColor">#F3F3F3</item> <item name="textColor_loginlist">#F3F3F3</item> <item name="textColor_login">#F3F3F3</item> <item name="textColor_settings">#fffafa</item> <item name="textColor_settings_sub">#F3F3F3</item> <item name="actionBarBackgroundColor">#007fff</item> <item name="cardview_bgColor">#2f4f4f</item> <item name="headviewColor">#252525</item> <item name="homepageColor">#2C2C2C</item> </style> </resources>
1.3在布局文件中使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="?attr/actionBarBackgroundColor"> </RelativeLayout> </RelativeLayout>从这个简单的布局文件中可以看到,我在内层RelativeLayout的控件上,分别使用了?attr/actoinBarBackgroundColor来引用主题中的颜色值。
1.4设置主题及布局文件
布局文件与主题都写好了,接下来我们就要在Activity的onCreate方法里使用了,在项目中使用的话可以写一个BaseActivity,在BaseActivity的onCreate方法中使用,其他的Activity直接继承他,就不用在每个Activity中书写相同的代码了。
AppThemeUtils这个类是自己写的
public class AppThemesUtils { private static AppThemesUtils mInstance = null; private AppThemesUtils() { } public static synchronized AppThemesUtils getmInstance() { if (mInstance == null) mInstance = new AppThemesUtils(); return mInstance; } public void onActivityCreateSetTheme(Activity activity) { switch (UIHelper.readThemeColorIndex()) { case "0": activity.setTheme(R.style.greenTheme); break; case "1": activity.setTheme(R.style.blueTheme); break; case "2": activity.setTheme(R.style.brownTheme); break; default: activity.setTheme(R.style.greenTheme); break; } } }而UIHelper.readThemeColorIndex()这个sp文件是在启动app的时候就从sd卡中的配置文件中通过解析json得到的配置参数:
String configFile = getFromSDcard(Contents.STOREMESSAGE); try { JSONObject extrasJson = new JSONObject(configFile); String themeColorIndex = extrasJson.getString(Contents.Common.THEMECOLOR); UIHelper.saveThemeColorIndex(themeColorIndex);} catch (Exception e) { ToastUtil.showMessage(SplashActivity.this, R.string.getStoreMessageFailed); e.printStackTrace(); }
public String getFromSDcard(String fileName) { String result = ""; File file = new File(Environment.getExternalStorageDirectory() + "/.myColor" + File.separator + fileName); if (!file.exists()) { return null; } InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); int lenght = in.available(); byte[] buffer = new byte[lenght]; in.read(buffer); result = EncodingUtils.getString(buffer, Contents.UTF8); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }以上只是一些简单的换肤(换颜色)功能;只要了解了就能一通百通了。
Demo下载 访问密码 a478