溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android怎么給應(yīng)用定制皮膚

發(fā)布時間:2022-01-12 20:08:45 來源:億速云 閱讀:135 作者:iii 欄目:移動開發(fā)

本文小編為大家詳細介紹“Android怎么給應(yīng)用定制皮膚”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Android怎么給應(yīng)用定制皮膚”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習新知識吧。

先說思路:

1)皮膚也就是相關(guān)的資源文件單獨放置在某個工程中,一種皮膚一個工程文件.一個工程包括N多的資源文件,多個工程間資源的關(guān)系是,文件名,資源ID等完全一樣.不同的可能是圖片資源,style等的設(shè)置不一樣.

皮膚工程在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對應(yīng)的Context,然后通過返回的context對象就可以訪問到com.eric.blackskin中的任何資源,如同訪問自身的資源一樣.

注:記得先安裝皮膚工程對應(yīng)的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;                                 }                         }                 });         } }

讀到這里,這篇“Android怎么給應(yīng)用定制皮膚”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI