閱讀400 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android動態切換主題

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

1) 軟件內置多個皮膚,不可由用戶增加或修改;

最低的自由度,軟件實現相對於後兩種最容易。

2) 官方提供皮膚供下載,用戶可以使用下載的皮膚;

用戶可選擇下載自己喜歡的皮膚,有些玩家會破解皮膚的定製方法,自己做皮膚使用,或者傳到網上給大家用。

參考:https://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()

一些參考資料:
https://www.eoeandroid.com/forum.php?mod=viewthread&tid=160087
https://www.eoeandroid.com/forum.php?mod=viewthread&tid=103123
android自帶主題和樣式








接下來來看範例:

設有一個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"
color:#ff0000;">pageBackground">
main.xml中引用白色主題還是藍色主題的pageBackground,交由代碼處理。動態切換主題。

代碼實現動態切換:
創建一個util類,設置一個全局變量保存主題信息。
那麼就必須調用actvity.finish()。然後再重新加載setTheme()

下麵貼出主要的代碼:
package irdc.ex03_21; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class EX03_21 extends Activity implements OnClickListener{ 
  /** Called when the activity is first created. */ 
  Button button = null;
  @Override 
  public void onCreate(Bundle savedInstanceState) {
    
    Utils.onActivityCreateSetTheme(this);
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    
    findViewById(R.id.button1).setOnClickListener(this);
    findViewById(R.id.button2).setOnClickListener(this);
    findViewById(R.id.button3).setOnClickListener(this);
    
  }
  @Override
  public void onClick(View v)
  {
    System.out.println("單擊按鈕");
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case R.id.button1:
      System.out.println("主題1");
      Utils.changeToTheme(this, 1);
      break;
    case R.id.button2:
      System.out.println("主題2");
      Utils.changeToTheme(this, 2);
      break;
    case R.id.button3:
      System.out.println("主題3");
      Utils.changeToTheme(this, 3);
      break;
    }
    
  }
}

package irdc.ex03_21;

import android.app.Activity;
import android.content.Intent;

public class Utils
{
	private static int sTheme;

	public final static int THEME_DEFAULT = 0;
	public final static int THEME_WHITE = 1;
	public final static int THEME_BLUE = 2;

	/**
	 * Set the theme of the Activity, and restart it by creating a new Activity
	 * of the same type.
	 */
	public static void changeToTheme(Activity activity, int theme)
	{
		sTheme = theme;
		activity.finish();

		activity.startActivity(new Intent(activity, activity.getClass()));
	}

	/** Set the theme of the activity, according to the configuration. */
	public static void onActivityCreateSetTheme(Activity activity)
	{
		switch (sTheme)
		{
		default:
		case 1:
		  activity.setTheme(R.style.Theme_Translucent);
			break;
		case 2:
			activity.setTheme(R.style.Theme_Translucent2);
			break;
		case 3:
			activity.setTheme(R.style.Theme_Transparent);
			break;
		}
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:andro
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
  android:textColor="@drawable/darkgreen"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/str_text_view1"
  />

  <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主題1" />

  <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主題2" />

  <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主題3" />

</LinearLayout>




最後更新:2017-04-02 17:28:36

  上一篇:go ASP.NET報錯——Master Page frameworkName: Value cannot be null error
  下一篇:go 筆記本鍵盤失靈