android程序如何应用多种主题风格

xiaoxiao2021-03-01  21

原理:主程序与主题apk使用相同的sharedUserId, 使得主程序可以访问主题apk包中的资源   主程序:MainSkin.apk   主题包:SkinTest1.apk, SkinTest2.apk   MainSkin实现一个activity来加载SkinTest1或SkinTest2中的资源,给自己设置不同主题的背景图片和按钮图片   MainSkin的AndroidManifest.xml如下: MainSkin的Acitivity定义: package com.huyuke.mainskin; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundExce ption; import android.content.pm.PackageInfo; import android.content.res.Resources; import android.os.Bundle; import android.os.Process; import android.widget.LinearLayout; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; import android.util.Log; import java.util.ArrayList; public class MainSkinActivity extends Activity implements OnClickListener { private static final String TAG = "MainSkinActivity"; private LinearLayout mBg; private Button mBtn; private Resources defaultRes = null; private ArrayList mSkinPkgs = new ArrayList(); private int mCurrSkinIndex = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle(R.string.theme_name); mBg = (LinearLayout) findViewById(R.id.linear_layout_1); mBtn = (Button) findViewById(R.id.btn_change_skin); mBg.setBackgroundResource(R.drawable.bg); mBtn.setBackgroundResource(R.drawable.btn_normal); mBtn.setOnClickListener(this); getSkinPackages(); } public void onClick(View v) { String skinPkgToSet= mSkinPkgs.get(mCurrSkinIndex); mCurrSkinIndex++; if (mCurrSkinIndex == mSkinPkgs.size()) { mCurrSkinIndex = 0; } applySkin(skinPkgToSet); } private void applySkin(String skinPkg){ Context skinContext; try { skinContext = this.createPackageContext(skinPkg, Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { Log.e(TAG, "createPackageContext exception: " + e); e.printStackTrace(); return; } Resources res = skinContext.getResources(); /* OR try { res = getPackageManager().getResourcesForApplication(ski nPkg); } catch (NameNotFoundException e) { Log.e(TAG, "getResourcesForApplication exception: " + e); return; } */ mBg.setBackgroundDrawable(res.getDrawable(res.getI dentifier("bg", "drawable", skinPkg))); mBtn.setBackgroundDrawable(res.getDrawable(res.get Identifier("btn_normal", "drawable", skinPkg))); setTitle(res.getString(res.getIdentifier("theme_na me", "string", skinPkg))); } private void getSkinPackages() { String[] skinPkgs = getPackageManager().getPackagesForUid(Process.myUi d()); String self = getPackageName(); for (String skinPackage: skinPkgs) { Log.i(TAG, "package for this user: " + skinPackage); if (!(self.equals(skinPackage))) { Log.i(TAG, "this is a skin package"); mSkinPkgs.add(skinPackage); } } Log.i(TAG, "I have " + mSkinPkgs.size() + " skins"); } } 同时MainSkin中需要放一套默认图片资源   主题包中不需要代码,只需要资源文件和在manifest里面声明和主程序MainSkin使用相同的sharedUserId即可,   在这两个主题包中分别放了两套不同的图片,每套图包括一张activity背景图和一张button背景图   如SkinTest1的AndroidManifest.xml内容如下: 显示效果:      
转载请注明原文地址: https://www.6miu.com/read-4150258.html

最新回复(0)