换肤实现思路(二)

以前写过一个关于皮肤的换肤思路的博客。但那个里边主要是针对于资源换肤,没有布局换肤的概念。今天实现了一个可以更换布局的换肤实现。

 

思路还是上篇文章的思路,只是做了一个简单的实现。

 

代码如下:

package com.test.android.skin.main;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView((View)getResource("main", "layout"));
		Button btn = (Button) findViewById(R.id.btn);
		btn.setBackgroundDrawable((Drawable) getResource("btn_background", "drawable"));
		
		SqLHelper helper = new SqLHelper(this);
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("id", "3");
		values.put("packname", "org.yangzc.package3");
		db.insert("skinTab", null, values);
		db.close();
		
		db = helper.getWritableDatabase();
		Cursor c = db.rawQuery("select * from skinTab", null);
        while(c.moveToNext()){
        	String id = c.getString(c.getColumnIndex("id"));
        	String packname = c.getString(c.getColumnIndex("packname"));
        	Log.d("Tag", "Id:"+id + "   packname:" + packname);
        }
	}
	
	private Object getResource(String filename, String defType){
		Context myContext = null;
		try {
			myContext = this.createPackageContext(
					"com.test.android.skin.myskin",
					Context.CONTEXT_IGNORE_SECURITY);
			int id = myContext.getResources().getIdentifier(filename, defType, myContext.getPackageName());
			if("drawable".equals(defType)){
				return myContext.getResources().getDrawable(id);
			}else if("layout".equals(defType)){
				LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(LAYOUT_INFLATER_SERVICE);
				return inflater.inflate(id, null);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
}

 

 

方向一:

这几天看了一下源码,主要是关于Context的东西。理想化的情况是,进入activity之后首先改一下context,这样之后框架就会动态实现换肤功能。追到了ContextThemeWrapper类,本来看到了attachBaseContext方法,以为OK了呢。看到一下代码郁闷了。继续看...

protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值