How to change skin

Source: Internet
Author: User
To make a good program, skin is also a very important part, because it is a part that directly attracts users. Successful software such as QQ, MSN, and sogou input methods all have a large amount of skin for users to choose from.
You can set the theme and style of the program. However, skin replacement is often more than that. It may need to replace more resources. This program only describes how to change the skin, the code is very simple. Includes two projects, one main project and one skin project, responsible for skin replacement.
The code is described as follows:
1. Main Program:
Package com. d_skinmain;

Import Android. App. activity;
Import Android. content. context;
Import Android. content. PM. packagemanager. namenotfoundexception;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. imagebutton;
Import Android. widget. linearlayout;
Import Android. widget. textview;

Public class d_skinmain extends activity {
/** Called when the activity is first created .*/
Private linearlayout showbg;
Private context friendcontext = NULL;
Private int flag = 0;
Private textview Info;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Info = (textview) findviewbyid (r.id.info );
Info. settext ("skin1 ");
Showbg = (linearlayout) findviewbyid (R. Id. showbg );
Showbg. setbackgroundresource (R. drawable. Background );

Try {
Friendcontext = This. createpackagecontext (
"Com. d_skin ",
Context. context_ignore_security );
} Catch (namenotfoundexception e ){
E. printstacktrace ();
}

Button BTN = (button) findviewbyid (R. Id. BTN );
BTN. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
If (flag = 0 ){
Showbg. setbackgrounddrawable (friendcontext. getresources (). getdrawable (R. drawable. Background ));
Flag = 1;
Info. settext ("skin2 ");
} Else if (flag = 1 ){
Showbg. setbackgroundresource (R. drawable. Background );
Flag = 0;
Info. settext ("skin1 ");
}

}

});
}
}

2 skin program:
Package com. d_skin;

Import Android. App. activity;
Import Android. OS. Bundle;

Public class d_skin extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
}
Hey, easy!

3. manifest. xml file
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. d_skin"
Android: versioncode = "1"
Android: versionname = "1.0"
Android: shareduserid = "com. d_skinmain.skin">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". d_skin"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>
<Uses-SDK Android: minsdkversion = "3"/>

</Manifest>

4. program description:
> To check the effect of this program, you must install the skin program. Otherwise, the program will pop up after you click the skin change button. I have not modified this so that you can easily check the effect of skin engineering.
> The program uses buttons to change the background image of the imageview. The image name is background.png. The two images are located in the two projects respectively. Click the skin replacement button of the main program to load the background image resources in the skin project for replacement, of course, there must be more than these resources in the case of skin replacement. Here we will describe the principle.
> The Key of the program is <Android: shareduserid = "com. d_skinmain.skin"> and shareduserid in menifest.
Multiple APK with the same user ID can be configured to run in the same process, so by default, it can access any data from each other. You can also configure it to run different processes and access the databases and files in the data directory of other APK files, just like accessing the data of the program.
> The key part of the code is: friendcontext = This. createpackagecontext (
"Com. d_skin ",
Context. context_ignore_security );

Showbg. setbackgrounddrawable (friendcontext. getresources (). getdrawable (R. drawable. Background ));

> Attach the packaging files of the two projects for you to view them. : Loveliness:
[This post was last edited by allen_fang] Attachment

D_skinmain.rar (203.83 KB)

, Downloads: 2
Main project

D_skin.rar (54.5 KB)

, Downloads: 3
Skin Engineering

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.