Android 給應用定製皮膚——-值得一看

來源:互聯網
上載者:User

在實現程式功能的同時,如果能讓程式介面更加美觀,有錦上添花之妙.
先說思路:
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檔案.

 

  1. public class main extends Activity {
  2.         /** Called when the activity is first created. */
  3.         private LinearLayout showBg;
  4.         private Button btn;
  5.         private Context green_skin_Context = null;
  6.         private Context black_skin_Context = null;
  7.         int flag = 0;
  8.         @Override
  9.         public void onCreate(Bundle savedInstanceState) {
  10.                 super.onCreate(savedInstanceState);
  11.                 setContentView(R.layout.main);
  12.                 showBg = (LinearLayout) findViewById(R.id.linear_layout_1);
  13.                 try {
  14.                         green_skin_Context = this.createPackageContext(
  15.                                         "com.eric.greenskin", Context.CONTEXT_IGNORE_SECURITY);
  16.                 } catch (NameNotFoundException e) {
  17.                         e.printStackTrace();
  18.                 }
  19.                 try {
  20.                         black_skin_Context = this.createPackageContext(
  21.                                         "com.eric.blackskin", Context.CONTEXT_IGNORE_SECURITY);
  22.                 } catch (NameNotFoundException e) {
  23.                         e.printStackTrace();
  24.                 }
  25.                 btn = (Button) findViewById(R.id.btn_change_skin);
  26.                 btn.setOnClickListener(new OnClickListener() {
  27.                         @Override
  28.                         public void onClick(View v) {
  29.                                 if (flag == 0) {
  30.                                         showBg.setBackgroundDrawable(green_skin_Context
  31.                                                         .getResources().getDrawable(R.drawable.bg));
  32.                                         btn.setBackgroundDrawable(green_skin_Context
  33.                                                         .getResources().getDrawable(R.drawable.btn_normal));
  34.                                         
  35.                                         flag = 1;
  36.                                 } else if (flag == 1) {
  37.                                         showBg.setBackgroundDrawable(black_skin_Context
  38.                                                         .getResources().getDrawable(R.drawable.bg));
  39.                                         btn.setBackgroundDrawable(black_skin_Context
  40.                                                         .getResources().getDrawable(R.drawable.btn_normal));
  41.                                         flag = 0;
  42.                                 }
  43.                         }
  44.                 });
  45.         }
  46. }

複製代碼

 black_skin.rar (159.54 KB) 
 green_skin.rar (253.09 KB) 
 SkinMain.rar (8.34 KB)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.