在實現程式功能的同時,如果能讓程式介面更加美觀,有錦上添花之妙.
先說思路:
1)皮膚也就是相關的資源檔單獨放置在某個工程中,一種皮膚一個工程檔案.一個工程包括N多的資源檔,多個工程間資源的關係是,檔案名稱,資源ID等完全一樣.不同的可能是圖片資源,style等的設定不一樣.
2)皮膚工程在AndroidManifest.xml中配置android:sharedUserId="com.eric.skinmain".
表明允許com.eric.skinmain訪問本工程中的資源檔. com.eric.skinmain是主專案的包名
3)主專案通過 this.createPackageContext("com.eric.blackskin",Context.CONTEXT_IGNORE_SECURITY);
擷取到com.eric.blackskin對應的Context,然後通過返回的context對象就可以訪問到com.eric.blackskin中的任何資源,如同訪問自身的資源一樣
註:記得先安裝皮膚工程對應的apk檔案
public class main extends Activity {
/** Called when the activity is first created. */
private LinearLayout showBg;
private Button btn;
private Context green_skin_Context = null;
private Context black_skin_Context = null;
int flag = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showBg = (LinearLayout) findViewById(R.id.linear_layout_1);
try {
green_skin_Context = this.createPackageContext(
"com.eric.greenskin", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
try {
black_skin_Context = this.createPackageContext(
"com.eric.blackskin", Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
btn = (Button) findViewById(R.id.btn_change_skin);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flag == 0) {
showBg.setBackgroundDrawable(green_skin_Context
.getResources().getDrawable(R.drawable.bg));
btn.setBackgroundDrawable(green_skin_Context
.getResources().getDrawable(R.drawable.btn_normal));
flag = 1;
} else if (flag == 1) {
showBg.setBackgroundDrawable(black_skin_Context
.getResources().getDrawable(R.drawable.bg));
btn.setBackgroundDrawable(black_skin_Context
.getResources().getDrawable(R.drawable.btn_normal));
flag = 0;
}
}
});
}
}