一直很想弄清楚好多应用中是如何实现换皮肤这项功能的,花了下午点时间,查了下资料也实现了个切换主题的Demo;
首先要感谢下这位大哥,参阅了下他写的文件 http://www.eoeandroid.com/forum-viewthread-tid-31756-highlight-%E7%9A%AE%E8%82%A4.html
好了,废话不多说了,该切换主题的demo里面一共实现了两个功能,其一,搜索已经安装的皮肤,其二,应用安装的皮肤。
主项目包名为org.leepood.skindemo,主题项目的包名为org.leepood.skin.blue,org.leepood.skin.red,等等,只要前缀是org.leepood.skin.就行。
首先是查找已安装主题的代码:
这段代码的含义就是去查找系统中安装的包名,若以org.leepood.skin.开头则说明该包为主题包,将其加入listview中显示出来。代码中使用了多线程避免时间过长堵塞UI。程序将当前主题配置保存在SharedPreference中,为SharedPreference注册了一个监听函数,当其值发生改变时自动调用新的样式。当然,这只是个demo而已,一开始加载Activity没有去读取主题,这个可以由大家自己去实现。
最后贴张图片:
Android实现主题切换机制2
昨天花了点时间实现了主题的切换,但是里面还是不够灵活,回去想了想可以用继承和回调函数来进一步灵活更改主题,现在记录下我的实现办法
首先一个自定义类ThemeActivity继承自Activity,这个类是以后所有Activity的父类,在这个类里面定义了一个接口
public interface OnThemeChangedListener
{
public void onChanged(String newThemePackageName);
}
接下来,首先是要给ThemeActivity注册一个主题切换的listener,代码如下:
public void setOnThemeChangedListener(OnThemeChangedListener listener)
{
this.listener=listener;
}
然后就是注册一个SharedPreference来监听xml的变化,当发生改变的时候自动去调用listener.onChanged方法,将新的主题包名传递过去,代码如下:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if(key.equals("themePackage"))
{
listener.onChanged(sp.getString("themePackage", ""));
}
}
接着在继承于ThemeActivity的子类里面首先是setOnThemeChangedListener.接着用一个匿名内部类搞定。
首先要感谢下这位大哥,参阅了下他写的文件 http://www.eoeandroid.com/forum-viewthread-tid-31756-highlight-%E7%9A%AE%E8%82%A4.html
好了,废话不多说了,该切换主题的demo里面一共实现了两个功能,其一,搜索已经安装的皮肤,其二,应用安装的皮肤。
主项目包名为org.leepood.skindemo,主题项目的包名为org.leepood.skin.blue,org.leepood.skin.red,等等,只要前缀是org.leepood.skin.就行。
首先是查找已安装主题的代码:
- package org.leepood.skindemo;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.ContextMenu;
- import android.view.LayoutInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.View.OnCreateContextMenuListener;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- public class Main extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener{
- private ListView listview;
- private Context c;
- private Handler mHandler;
- private ProgressDialog pDialog;
- private SkinAdapter adapter;
- private SharedPreferences sp;
- static final int MESSAGE_SEARCHED_SKIN=0;
- static final int MESSAGE_SEARCHING_SKIN=MESSAGE_SEARCHED_SKIN+1;
- static final int MESSAGE_SEARCHED_SKIN_FOR_NONTHING=MESSAGE_SEARCHING_SKIN+1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- pDialog.show();
- new Thread(serachSkin).start();
- }
- private void init()
- {
- c=this;
- mHandler=new Handler(){
- @Override
- public void handleMessage(Message msg) {
- switch(msg.what)
- {
- case MESSAGE_SEARCHED_SKIN:
- ArrayList
- skins=(ArrayList
- ) msg.obj;//获取skins
- adapter=new SkinAdapter(c, skins);
- listview.setAdapter(adapter);
- Toast.makeText(c, "查找到已经安装的皮肤", 1).show();
- pDialog.dismiss();
- break;
- case MESSAGE_SEARCHED_SKIN_FOR_NONTHING:
- Toast.makeText(c, "未查找到任何皮肤", 1).show();
- pDialog.dismiss();
- }
- }
- };
- sp=this.getSharedPreferences("config",Context.MODE_WORLD_WRITEABLE);
- sp.registerOnSharedPreferenceChangeListener(this);
- listview=(ListView) findViewById(R.id.list);
- listview.setItemsCanFocus(false);
- listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
- pDialog=new ProgressDialog(this);
- pDialog.setMessage("正在查找已经安装的皮肤");
- listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- menu.add("使用该主题");
- }
- });
- }
- private Runnable serachSkin =new Runnable(){
- public void run() {
- PackageManager manager=c.getPackageManager();
- List
- packages=manager.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
- ArrayList
- skins=new ArrayList
- ();
- for(PackageInfo info:packages)
- {
- //System.out.println(info.packageName);
- if(info.packageName.startsWith("org.leepood.skin."))
- {
- skins.add(info);
- }
- }
- if(skins.size()>0)
- {
- Message msg=mHandler.obtainMessage();
- msg.obj=skins;
- msg.what=MESSAGE_SEARCHED_SKIN;
- mHandler.sendMessage(msg);
- }
- else
- {
- mHandler.sendEmptyMessage(MESSAGE_SEARCHED_SKIN_FOR_NONTHING);
- }
- }
- };
- private class SkinAdapter extends BaseAdapter
- {
- LayoutInflater mInflater;
- ArrayList
- datas;
- PackageManager manager;
- public SkinAdapter(Context c,ArrayList
- datas)
- {
- this.datas=datas;
- mInflater=LayoutInflater.from(c);
- manager=c.getPackageManager();
- }
- public int getCount() {
- return datas.size();
- }
- public Object getItem(int position) {
- return datas.get(position);
- }
- public long getItemId(int position) {
- return 0;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- if(convertView==null)
- {
- convertView=mInflater.inflate(R.layout.skin_item, null);
- }
- ImageView icon=(ImageView) convertView.findViewById(R.id.skin_icon);
- TextView skin_name=(TextView) convertView.findViewById(R.id.skin_name);
- PackageInfo info=datas.get(position);
- icon.setImageDrawable(info.applicationInfo.loadIcon(manager));
- skin_name.setText(info.applicationInfo.loadLabel(manager));
- return convertView;
- }
- }
- public void onThemeChanged(String newThemePackageName) {
- try {
- Context themeContext=this.createPackageContext(newThemePackageName, CONTEXT_IGNORE_SECURITY);
- Resources res=themeContext.getResources();
- setControlsStyle(res);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- }
- private void setControlsStyle(Resources res)
- {
- listview.setBackgroundColor(res.getColor(R.color.ListView_bg));
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterContextMenuInfo menuInfo=(AdapterContextMenuInfo)item.getMenuInfo();
- PackageInfo info=(PackageInfo) adapter.getItem(menuInfo.position);
- sp.edit().putString("themePackage", info.packageName).commit();
- return super.onContextItemSelected(item);
- }
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
- String key) {
- System.out.println("themeChange");
- onThemeChanged(sharedPreferences.getString(key, ""));
- }
- }
package org.leepood.skindemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Main extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener{
private ListView listview;
private Context c;
private Handler mHandler;
private ProgressDialog pDialog;
private SkinAdapter adapter;
private SharedPreferences sp;
static final int MESSAGE_SEARCHED_SKIN=0;
static final int MESSAGE_SEARCHING_SKIN=MESSAGE_SEARCHED_SKIN+1;
static final int MESSAGE_SEARCHED_SKIN_FOR_NONTHING=MESSAGE_SEARCHING_SKIN+1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
pDialog.show();
new Thread(serachSkin).start();
}
private void init()
{
c=this;
mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what)
{
case MESSAGE_SEARCHED_SKIN:
ArrayList
skins=(ArrayList
) msg.obj;//获取skins
adapter=new SkinAdapter(c, skins);
listview.setAdapter(adapter);
Toast.makeText(c, "查找到已经安装的皮肤", 1).show();
pDialog.dismiss();
break;
case MESSAGE_SEARCHED_SKIN_FOR_NONTHING:
Toast.makeText(c, "未查找到任何皮肤", 1).show();
pDialog.dismiss();
}
}
};
sp=this.getSharedPreferences("config",Context.MODE_WORLD_WRITEABLE);
sp.registerOnSharedPreferenceChangeListener(this);
listview=(ListView) findViewById(R.id.list);
listview.setItemsCanFocus(false);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
pDialog=new ProgressDialog(this);
pDialog.setMessage("正在查找已经安装的皮肤");
listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add("使用该主题");
}
});
}
private Runnable serachSkin =new Runnable(){
public void run() {
PackageManager manager=c.getPackageManager();
List
packages=manager.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
ArrayList
skins=new ArrayList
();
for(PackageInfo info:packages)
{
//System.out.println(info.packageName);
if(info.packageName.startsWith("org.leepood.skin."))
{
skins.add(info);
}
}
if(skins.size()>0)
{
Message msg=mHandler.obtainMessage();
msg.obj=skins;
msg.what=MESSAGE_SEARCHED_SKIN;
mHandler.sendMessage(msg);
}
else
{
mHandler.sendEmptyMessage(MESSAGE_SEARCHED_SKIN_FOR_NONTHING);
}
}
};
private class SkinAdapter extends BaseAdapter
{
LayoutInflater mInflater;
ArrayList
datas;
PackageManager manager;
public SkinAdapter(Context c,ArrayList
datas)
{
this.datas=datas;
mInflater=LayoutInflater.from(c);
manager=c.getPackageManager();
}
public int getCount() {
return datas.size();
}
public Object getItem(int position) {
return datas.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.skin_item, null);
}
ImageView icon=(ImageView) convertView.findViewById(R.id.skin_icon);
TextView skin_name=(TextView) convertView.findViewById(R.id.skin_name);
PackageInfo info=datas.get(position);
icon.setImageDrawable(info.applicationInfo.loadIcon(manager));
skin_name.setText(info.applicationInfo.loadLabel(manager));
return convertView;
}
}
public void onThemeChanged(String newThemePackageName) {
try {
Context themeContext=this.createPackageContext(newThemePackageName, CONTEXT_IGNORE_SECURITY);
Resources res=themeContext.getResources();
setControlsStyle(res);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private void setControlsStyle(Resources res)
{
listview.setBackgroundColor(res.getColor(R.color.ListView_bg));
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo=(AdapterContextMenuInfo)item.getMenuInfo();
PackageInfo info=(PackageInfo) adapter.getItem(menuInfo.position);
sp.edit().putString("themePackage", info.packageName).commit();
return super.onContextItemSelected(item);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
System.out.println("themeChange");
onThemeChanged(sharedPreferences.getString(key, ""));
}
}
这段代码的含义就是去查找系统中安装的包名,若以org.leepood.skin.开头则说明该包为主题包,将其加入listview中显示出来。代码中使用了多线程避免时间过长堵塞UI。程序将当前主题配置保存在SharedPreference中,为SharedPreference注册了一个监听函数,当其值发生改变时自动调用新的样式。当然,这只是个demo而已,一开始加载Activity没有去读取主题,这个可以由大家自己去实现。
最后贴张图片:
Android实现主题切换机制2
昨天花了点时间实现了主题的切换,但是里面还是不够灵活,回去想了想可以用继承和回调函数来进一步灵活更改主题,现在记录下我的实现办法
首先一个自定义类ThemeActivity继承自Activity,这个类是以后所有Activity的父类,在这个类里面定义了一个接口
public interface OnThemeChangedListener
{
public void onChanged(String newThemePackageName);
}
接下来,首先是要给ThemeActivity注册一个主题切换的listener,代码如下:
public void setOnThemeChangedListener(OnThemeChangedListener listener)
{
this.listener=listener;
}
然后就是注册一个SharedPreference来监听xml的变化,当发生改变的时候自动去调用listener.onChanged方法,将新的主题包名传递过去,代码如下:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if(key.equals("themePackage"))
{
listener.onChanged(sp.getString("themePackage", ""));
}
}
接着在继承于ThemeActivity的子类里面首先是setOnThemeChangedListener.接着用一个匿名内部类搞定。