Android app Custom Skin

Source: Internet
Author: User

While implementing the program functions, if you can make the program interface more beautiful, there is a icing on the cake.
First thought:
1) the skin, that is, the related resource files, are placed separately in a project, one skin and one engineering file. A project contains more than N resource files. The relationship between resources between multiple projects is that the file names and resource IDs are identical. different settings may be different for image resources and styles.
2) Configure Android: shareduserid = "com. Eric. skinmain" in androidmanifest. xml ".
Indicates that allow com. Eric. skinmain to access the resource file. com. Eric. skinmain in this project is the package name of the main project.
3) The main project uses this. createpackagecontext ("com. Eric. blackskin", context. context_ignore_security );
Obtain the context corresponding to com. Eric. blackskin, and then access any resources in COM. Eric. blackskin through the returned context object, just like accessing its own resources.

Note: Remember to first install the APK file corresponding to the skin Project



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

}

});
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.