Android动态切换主题

原文链接:http://blog.csdn.net/wsscy2004/article/details/7562909

软件换肤从功能上可以划分三种:

1) 软件内置多个皮肤,不可由用户增加或修改;

最低的自由度,软件实现相对于后两种最容易。

2) 官方提供皮肤供下载,用户可以使用下载的皮肤;

用户可选择下载自己喜欢的皮肤,有些玩家会破解皮肤的定制方法,自己做皮肤使用,或者传到网上给大家用。

参考:http://blog.csdn.net/zhyooo123/article/details/6697186

3) 官方提供皮肤制作工具或方法,用户可自制皮肤。



关于主题和样式:

就像style一样,主题依然在<style>元素里边申明,也是以同样的方式引用。
不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。

@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过

关于设置主题的注意事项:
不少同学会发泄setTheme()竟然会无效。那么注意
使用setTheme()只能在Oncreate()之前使用。在setContentView(),还是不行那么就在super.onCreate(savedInstanceState);之前
如果要使用动态切换主题,那么就必须调用actvity.finish()。然后再重新加载setTheme()

一些参考资料:








接下来来看范例:

设有一个main.xml布局文件。
新建一个xml用于放置多个主题。如:

<--蓝色主题-->
<style name="Theme.Blue">
<item name=" pageBackground">@style/page_background_bl</item>
<item name="pagePaddingLayout">@style/page_padding_layout_bl</item>
</style>

<--白色主题-->
<style name="Theme.White">
<item name=" pageBackground">@style/page_background_wh</item>
<item name="pagePaddingLayout">@style/page_padding_layout_wh</item> </style>

注意到这里每个主题中的item名字是相同的,且在布局文件main.xml中
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="? pageBackground">
main.xml中引用白色主题还是蓝色主题的pageBackground,交由代码处理。动态切换主题。

代码实现动态切换:
创建一个util类,设置一个全局变量保存主题信息。
那么就必须调用actvity.finish()。然后再重新加载setTheme()

下面贴出主要的代码:
[java]  view plain copy
  1. package irdc.ex03_21;   
  2.   
  3.   
  4. import android.app.Activity;   
  5. import android.os.Bundle;   
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10.   
  11. public class EX03_21 extends Activity implements OnClickListener{   
  12.   /** Called when the activity is first created. */   
  13.   Button button = null;  
  14.   @Override   
  15.   public void onCreate(Bundle savedInstanceState) {  
  16.       
  17.     Utils.onActivityCreateSetTheme(this);  
  18.     super.onCreate(savedInstanceState);   
  19.     setContentView(R.layout.main);   
  20.       
  21.     findViewById(R.id.button1).setOnClickListener(this);  
  22.     findViewById(R.id.button2).setOnClickListener(this);  
  23.     findViewById(R.id.button3).setOnClickListener(this);  
  24.       
  25.   }  
  26.   @Override  
  27.   public void onClick(View v)  
  28.   {  
  29.     System.out.println("单击按钮");  
  30.     // TODO Auto-generated method stub  
  31.     switch (v.getId())  
  32.     {  
  33.     case R.id.button1:  
  34.       System.out.println("主题1");  
  35.       Utils.changeToTheme(this1);  
  36.       break;  
  37.     case R.id.button2:  
  38.       System.out.println("主题2");  
  39.       Utils.changeToTheme(this2);  
  40.       break;  
  41.     case R.id.button3:  
  42.       System.out.println("主题3");  
  43.       Utils.changeToTheme(this3);  
  44.       break;  
  45.     }  
  46.       
  47.   }  
  48. }  

[java]  view plain copy
  1. package irdc.ex03_21;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5.   
  6. public class Utils  
  7. {  
  8.     private static int sTheme;  
  9.   
  10.     public final static int THEME_DEFAULT = 0;  
  11.     public final static int THEME_WHITE = 1;  
  12.     public final static int THEME_BLUE = 2;  
  13.   
  14.     /** 
  15.      * Set the theme of the Activity, and restart it by creating a new Activity 
  16.      * of the same type. 
  17.      */  
  18.     public static void changeToTheme(Activity activity, int theme)  
  19.     {  
  20.         sTheme = theme;  
  21.         activity.finish();  
  22.   
  23.         activity.startActivity(new Intent(activity, activity.getClass()));  
  24.     }  
  25.   
  26.     /** Set the theme of the activity, according to the configuration. */  
  27.     public static void onActivityCreateSetTheme(Activity activity)  
  28.     {  
  29.         switch (sTheme)  
  30.         {  
  31.         default:  
  32.         case 1:  
  33.           activity.setTheme(R.style.Theme_Translucent);  
  34.             break;  
  35.         case 2:  
  36.             activity.setTheme(R.style.Theme_Translucent2);  
  37.             break;  
  38.         case 3:  
  39.             activity.setTheme(R.style.Theme_Transparent);  
  40.             break;  
  41.         }  
  42.     }  
  43. }  

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent"  
  7.   >  
  8.   <TextView  
  9.   android:textColor="@drawable/darkgreen"  
  10.   android:layout_width="fill_parent"   
  11.   android:layout_height="wrap_content"   
  12.   android:text="@string/str_text_view1"  
  13.   />  
  14.   
  15.   <Button  
  16.       android:id="@+id/button2"  
  17.       android:layout_width="wrap_content"  
  18.       android:layout_height="wrap_content"  
  19.       android:text="主题1" />  
  20.   
  21.   <Button  
  22.       android:id="@+id/button1"  
  23.       android:layout_width="wrap_content"  
  24.       android:layout_height="wrap_content"  
  25.       android:text="主题2" />  
  26.   
  27.   <Button  
  28.       android:id="@+id/button3"  
  29.       android:layout_width="wrap_content"  
  30.       android:layout_height="wrap_content"  
  31.       android:text="主题3" />  
  32.   
  33. </LinearLayout>  

[java]  view plain copy
  1. <img src="https://img-my.csdn.net/uploads/201205/13/1336912506_5495.png" alt="">  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值