Android進階之APK方式換膚

[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); 
        //默認皮膚  
        defaultbutton = (Button)findViewById(R.id.defaultButton); 
        defaultbutton.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) 
            { 
                layout.setBackgroundResource(R.drawable.netskin); 
            } 
        }); 
        //為其他皮膚添加點擊按鈕  
        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(); 
            } 
        } 
    } 
 
    /**
     * 獲取所有已安裝的皮膚主題
     * 
     * @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; 
    } 
 
    /**
     * 判斷是否是皮膚主題
     * 
     * @param packageName
     * @return
     */ 
    private boolean isSkinPackage(String packageName) 
    { 
        // 自己制作的皮膚主題包名 例如: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);
  //默認皮膚
  defaultbutton = (Button)findViewById(R.id.defaultButton);
  defaultbutton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v)
   {
    layout.setBackgroundResource(R.drawable.netskin);
   }
  });
  //為其他皮膚添加點擊按鈕
  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();
   }
  }
 }

 /**
  * 獲取所有已安裝的皮膚主題
  *
  * @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;
 }

 /**
  * 判斷是否是皮膚主題
  *
  * @param packageName
  * @return
  */
 private boolean isSkinPackage(String packageName)
 {
  // 自己制作的皮膚主題包名 例如: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="默認皮膚" /> 
 
</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="默認皮膚" />

</LinearLayout>

 

源代碼下載:http://up.aiwalls.com/2012/0428/20120428095448263.rar

 摘自 落日小屋
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *