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檔案 



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;
                                }

                        }

                });
        }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.