• Android进阶2之APK方式换肤



    1. public class MainActivity extends Activity  
    2. {  
    3.     private Button defaultbutton = null;  
    4.     @Override  
    5.     public void onCreate(Bundle savedInstanceState)  
    6.     {  
    7.         super.onCreate(savedInstanceState);  
    8.         setContentView(R.layout.main);  
    9.         final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);  
    10.         //默认皮肤  
    11.         defaultbutton = (Button)findViewById(R.id.defaultButton);  
    12.         defaultbutton.setOnClickListener(new OnClickListener() {  
    13.             @Override  
    14.             public void onClick(View v)  
    15.             {  
    16.                 layout.setBackgroundResource(R.drawable.netskin);  
    17.             }  
    18.         });  
    19.         //为其他皮肤添加点击按钮  
    20.         ArrayList<PackageInfo> skinList = getAllSkin();  
    21.         for (int i = 0; i < skinList.size(); i++)  
    22.         {  
    23.             try  
    24.             {  
    25.                 final Context context = createPackageContext(skinList.get(i).packageName,  
    26.                         Context.CONTEXT_IGNORE_SECURITY);  
    27.                 Button btn = new Button(this);  
    28.                 btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
    29.                 btn.setText(context.getText(R.string.app_name));  
    30.                 btn.setOnClickListener(new OnClickListener() {  
    31.                     @Override  
    32.                     public void onClick(View v)  
    33.                     {  
    34.                         layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.netskin));  
    35.                     }  
    36.                 });  
    37.                 layout.addView(btn);  
    38.             }  
    39.             catch (NameNotFoundException e)  
    40.             {  
    41.                 e.printStackTrace();  
    42.             }  
    43.         }  
    44.     }  
    45.   
    46.     /** 
    47.      * 获取所有已安装的皮肤主题 
    48.      *  
    49.      * @return 
    50.      */  
    51.     private ArrayList<PackageInfo> getAllSkin()  
    52.     {  
    53.         ArrayList<PackageInfo> skinList = new ArrayList<PackageInfo>();  
    54.         List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);  
    55.         for (PackageInfo p : packs)  
    56.         {  
    57.             if (isSkinPackage(p.packageName))  
    58.             {  
    59.                 skinList.add(p);  
    60.             }  
    61.         }  
    62.         return skinList;  
    63.     }  
    64.   
    65.     /** 
    66.      * 判断是否是皮肤主题 
    67.      *  
    68.      * @param packageName 
    69.      * @return 
    70.      */  
    71.     private boolean isSkinPackage(String packageName)  
    72.     {  
    73.         // 自己制作的皮肤主题包名 例如:sunlight.skin0 sunlight.skin1等等  
    74.         String rex = "sunlight.skin\w";  
    75.         Pattern pattern = Pattern.compile(rex);  
    76.         Matcher matcher = pattern.matcher(packageName);  
    77.         return matcher.find();  
    78.     }  
    79. }  


    main.xml

      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:id="@+id/layout"  
      4.     android:layout_width="fill_parent"  
      5.     android:layout_height="fill_parent"  
      6.     android:orientation="vertical" >  
      7.   
      8.     <Button  
      9.         android:id="@+id/defaultButton"  
      10.         android:layout_width="fill_parent"  
      11.         android:layout_height="wrap_content"  
      12.         android:text="默认皮肤" />  
      13.   
      14. </LinearLayout>
  • 相关阅读:
    linux 常用命令
    git 常见命令
    合并两个有序链表---python
    Code Contract for .NET
    Kruskal最小生成树算法
    逻辑-哲学
    停机问题
    逆向工程
    .net framework
    python 类库
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3573510.html
Copyright © 2020-2023  润新知