android 类似QQ 换皮肤 实现思路 apk资源共享

1、首先在AndroidManifest.xml中的<manifest>中加入android:sharedUserId="共享id"----具有相同id的apk可以不受限制访问。

2、根据包名创建Context,在Activity下有提供方法createPackageContext能够依据包名创建。

3、获取共享的Apk资源。

注意:

1、Activity中的findViewById()与View中的findViewById()区别在于:

Activity需要对setContextView()后的Layout才能由findViewById()获得View。

View只需要有对象即可由findViewById()获得在View中包含id的View。

2、在资源获取方面主要获取那个apk下的资源主要是由Context决定。

在资源apk没安装前:

资源文件安装后:

主apk包名:com.app.share;

资源apk包名:com.app.share2;

主Activity中获取资源apk中R,并且生成Layout对id为button1的按钮设置监听。

Java代码 复制代码 收藏代码

  1. public class StartAct extends Activity {    
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {    
  5. super.onCreate(savedInstanceState);    
  6. //        setContentView(R.layout.main);  
  7.         Button btn = new Button(this);    
  8.         btn.setText("TO SECOND");    
  9.         btn.setOnClickListener(new OnClickListener() {    
  10. @Override
  11. public void onClick(View v) {    
  12. // TODO Auto-generated method stub  
  13.                 Intent intent = new Intent(StartAct.this, SecontAct.class);    
  14.                 StartAct.this.startActivity(intent);    
  15.             }    
  16.         });    
  17. this.setContentView(btn);    
  18. try {       
  19.             Context other = this.createPackageContext("com.app.share2", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);    
  20.             Class<?> c = other.getClassLoader().loadClass("com.app.share2.R");    
  21.             Class<?>[] cl = c.getClasses();    
  22. int b =0;    
  23. for (int i = 0; i < cl.length; i++) {    
  24.                 Log.d("TAG", cl[i].getSimpleName());    
  25.                 Field field[] = cl[i].getFields();    
  26. for (int j = 0; j &lt; field.length; j++) {    
  27.                     Log.d("TAG", "NAME:"+field[j].getName()+"--VALUE:"+field[j].getInt(field[j].getName()));    
  28. if(field[j].getName().equals("button1")) {    
  29.                         b = field[j].getInt(field[j].getName());    
  30.                         Log.d("TAG", "--------id");    
  31.                     }    
  32.                 };    
  33.             }    
  34.             View v = LayoutInflater.from(other).inflate(R.layout.main, null);    
  35.             Button btn1 = (Button) v.findViewById(b);    
  36.             btn1.setOnClickListener(new OnClickListener() {    
  37. @Override
  38. public void onClick(View v) {    
  39. // TODO Auto-generated method stub  
  40.                     Log.d("TAG", "BUTTON FROM Share2");    
  41.                     Toast.makeText(StartAct.this, "BUTTON FROM Share2", Toast.LENGTH_SHORT).show();    
  42.                 }    
  43.             });    
  44. this.setContentView(v);    
  45.         } catch (NameNotFoundException e) {    
  46. // TODO Auto-generated catch block  
  47.             e.printStackTrace();    
  48.         } catch (ClassNotFoundException e) {    
  49. // TODO Auto-generated catch block  
  50.             e.printStackTrace();    
  51.         } catch (IllegalArgumentException e) {    
  52. // TODO Auto-generated catch block  
  53.             e.printStackTrace();    
  54.         } catch (IllegalAccessException e) {    
  55. // TODO Auto-generated catch block  
  56.             e.printStackTrace();    
  57.         }    
  58.     }    
  59. }   
public class StartAct extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); Button btn = new Button(this); btn.setText("TO SECOND"); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(StartAct.this, SecontAct.class); StartAct.this.startActivity(intent); } }); this.setContentView(btn); try { Context other = this.createPackageContext("com.app.share2", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE); Class&lt;?> c = other.getClassLoader().loadClass("com.app.share2.R"); Class<?>[] cl = c.getClasses(); int b =0; for (int i = 0; i < cl.length; i++) { Log.d("TAG", cl[i].getSimpleName()); Field field[] = cl[i].getFields(); for (int j = 0; j &lt; field.length; j++) { Log.d("TAG", "NAME:"+field[j].getName()+"--VALUE:"+field[j].getInt(field[j].getName())); if(field[j].getName().equals("button1")) { b = field[j].getInt(field[j].getName()); Log.d("TAG", "--------id"); } }; } View v = LayoutInflater.from(other).inflate(R.layout.main, null); Button btn1 = (Button) v.findViewById(b); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("TAG", "BUTTON FROM Share2"); Toast.makeText(StartAct.this, "BUTTON FROM Share2", Toast.LENGTH_SHORT).show(); } }); this.setContentView(v); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

资源apk包Layout

Xml代码 复制代码 收藏代码

  1. &lt;?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_;fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_;fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Share2"
  11. />
  12. <Button android:text="Button" android:id="@+id/button1" android:layout_;wrap_content" android:layout_height="wrap_content"></Button>
  13. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_;fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Share2" /> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>

你可能感兴趣的:(android,移动开发,包,资源,休闲)