Android advanced APK method skin replacement

Source: Internet
Author: User




[Java] public class MainActivity extends Activity
{
Private Button defaultbutton = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final LinearLayout layout = (LinearLayout) findViewById (R. id. layout );
// Default skin
Defaultbutton = (Button) findViewById (R. id. defaultButton );
Defaultbutton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v)
{
Layout. setBackgroundResource (R. drawable. netskin );
}
});
// Add a button for other skins
ArrayList <PackageInfo> skinList = getAllSkin ();
For (int I = 0; I <skinList. size (); I ++)
{
Try
{
Final Context context = createPackageContext (skinList. get (I). packageName,
Context. CONTEXT_IGNORE_SECURITY );
Button btn = new Button (this );
Btn. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
Btn. setText (context. getText (R. string. app_name ));
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v)
{
Layout. setBackgroundDrawable (context. getResources (). getDrawable (R. drawable. netskin ));
}
});
Layout. addView (btn );
}
Catch (NameNotFoundException e)
{
E. printStackTrace ();
}
}
}
 
/**
* Obtain all installed skin themes
*
* @ Return
*/
Private ArrayList <PackageInfo> getAllSkin ()
{
ArrayList <PackageInfo> skinList = new ArrayList <PackageInfo> ();
List <PackageInfo> packs = getPackageManager (). getInstalledPackages (0 );
For (PackageInfo p: packs)
{
If (isSkinPackage (p. packageName ))
{
SkinList. add (p );
}
}
Return skinList;
}
 
/**
* Determine if it is a skin topic
*
* @ Param packageName
* @ Return
*/
Private boolean isSkinPackage (String packageName)
{
// Name of your own skin theme package, for example, sunlight. skin0 sunlight. skin1
String rex = "sunlight. skin \ w ";
Pattern pattern = Pattern. compile (rex );
Matcher matcher = pattern. matcher (packageName );
Return matcher. find ();
}
}
Public class MainActivity extends Activity
{
Private Button defaultbutton = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final LinearLayout layout = (LinearLayout) findViewById (R. id. layout );
// Default skin
Defaultbutton = (Button) findViewById (R. id. defaultButton );
Defaultbutton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v)
{
Layout. setBackgroundResource (R. drawable. netskin );
}
});
// Add a button for other skins
ArrayList <PackageInfo> skinList = getAllSkin ();
For (int I = 0; I <skinList. size (); I ++)
{
Try
{
Final Context context = createPackageContext (skinList. get (I). packageName,
Context. CONTEXT_IGNORE_SECURITY );
Button btn = new Button (this );
Btn. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
Btn. setText (context. getText (R. string. app_name ));
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v)
{
Layout. setBackgroundDrawable (context. getResources (). getDrawable (R. drawable. netskin ));
}
});
Layout. addView (btn );
}
Catch (NameNotFoundException e)
{
E. printStackTrace ();
}
}
}

/**
* Obtain all installed skin themes
*
* @ Return
*/
Private ArrayList <PackageInfo> getAllSkin ()
{
ArrayList <PackageInfo> skinList = new ArrayList <PackageInfo> ();
List <PackageInfo> packs = getPackageManager (). getInstalledPackages (0 );
For (PackageInfo p: packs)
{
If (isSkinPackage (p. packageName ))
{
SkinList. add (p );
}
}
Return skinList;
}

/**
* Determine if it is a skin topic
*
* @ Param packageName
* @ Return
*/
Private boolean isSkinPackage (String packageName)
{
// Name of your own skin theme package, for example, sunlight. skin0 sunlight. skin1
String rex = "sunlight. skin \ w ";
Pattern pattern = Pattern. compile (rex );
Matcher matcher = pattern. matcher (packageName );
Return matcher. find ();
}
}
Main. xml


[Java] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/layout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/defaultButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Default skin"/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/layout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<Button
Android: id = "@ + id/defaultButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Default skin"/>

</LinearLayout>

 


Source code download: http://www.bkjia.com/uploadfile/2012/0428/20120428095448263.rar




From sunset hut
 

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.