注:以下所有言論純屬個人主觀分析。
換膚解決方案:
1.manifest.xml中設置android:sharedUserId,通過這種方式可以達到不用應用相互訪問資源的目的,以實現換膚功能。
優點:可定期提供換膚包供下載,換膚方式靈活。
缺點:如需使用某個皮膚,必須安裝該皮膚。
實現實例代碼:
package com.test.android.skin.main;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context friendContext = null;
try {
friendContext = this.createPackageContext(
"com.test.android.skin.myskin",
Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Button btn = (Button) findViewById(R.id.btn);
if (friendContext == null) {
// btn.setBackgroundResource(R.drawable.btn_background);
} else {
int id = friendContext.getResources().getIdentifier("btn_background", "drawable", friendContext.getPackageName());
btn.setBackgroundDrawable(friendContext.getResources().getDrawable(id));
}
}
}
2.一個應用中放置幾個固定的皮膚,皮膚之間的控制由程序來控制實現。
優點:實現簡單。
缺點:不夠靈活,要增加新的皮膚需要更新應用。
實現實例:這個很簡單,我覺得可以通過sharereference統一控制皮膚的前綴或者路徑來實現一個通用的換膚目的。
3.zip壓縮包式皮膚。應用可設置一個默認路徑。如果用戶選擇某個皮膚,則解壓該皮膚.zip到這個文件夾中。
優點:無需安裝,即使卸載掉皮膚應用,該皮膚仍然可用,不會影響正常使用。
缺點:可能會因爲用戶的誤操作,使皮膚文件損壞。
實現實例代碼:
核心代碼:解壓文件,讀文件
4.更改背景圖片達到整體換膚的目的。這種實現方式比較簡單。所有的activity都只有一個大背景,上邊的所有內容都爲透明樣式。這樣只需要更改一個背景顏色達到換膚的目的了。