当前位置:操作系统 > 安卓/Android >>

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
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,