Android動態切換主題

軟件換膚從功能上可以劃分三種:

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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  1. <img src="https://img-my.csdn.net/uploads/201205/13/1336912506_5495.png" alt="">  


http://blog.csdn.net/wsscy2004/article/details/7562909
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章