值得一看:Android給應用定製皮膚
在實現程序功能的同時,如果能讓程序界麵更加美觀,有錦上添花之妙.
先說思路:
1)皮膚也就是相關的資源文件單獨放置在某個工程中,一種皮膚一個工程文件.一個工程包括N多的資源文件,多個工程間資源的關係是,文件名,資源ID等完全一樣.不同的可能是圖片資源,style等的設置不一樣.
皮膚工程在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;
- }
- }
- });
- }
- }
最後更新:2017-04-03 18:51:50