- 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
- <?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>