zoukankan      html  css  js  c++  java
  • android 类似QQ 换皮肤 实现思路 apk资源共享

    tyle="margin:20px 0px 0px; padding:0px; line-height:26px; font-family:arial">


    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的按钮设置监听。

    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.              

    11.            @Override  

    12.            public void onClick(View v) {  

    13.                // TODO Auto-generated method stub  

    14.                Intent intent = new Intent(StartAct.this, SecontAct.class);  

    15.                StartAct.this.startActivity(intent);  

    16.            }  

    17.        });  

    18.        this.setContentView(btn);  

    19.        try {    

    20.            Context other = this.createPackageContext("com.app.share2", CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);  

    21.            Class<?> c = other.getClassLoader().loadClass("com.app.share2.R");  

    22.            Class<?>[] cl = c.getClasses();  

    23.            int b =0;  

    24.            for (int i = 0; i < cl.length; i++) {  

    25.                Log.d("TAG", cl[i].getSimpleName());  

    26.                Field field[] = cl[i].getFields();  

    27.                for (int j = 0; j < field.length; j++) {  

    28.                    Log.d("TAG""NAME:"+field[j].getName()+"--VALUE:"+field[j].getInt(field[j].getName()));  

    29.                    if(field[j].getName().equals("button1")) {  

    30.                        b = field[j].getInt(field[j].getName());  

    31.                        Log.d("TAG""--------id");  

    32.                    }  

    33.                };  

    34.            }  

    35.            View v = LayoutInflater.from(other).inflate(R.layout.main, null);  

    36.            Button btn1 = (Button) v.findViewById(b);  

    37.            btn1.setOnClickListener(new OnClickListener() {  

    38.                  

    39.                @Override  

    40.                public void onClick(View v) {  

    41.                    // TODO Auto-generated method stub  

    42.                    Log.d("TAG""BUTTON FROM Share2");  

    43.                    Toast.makeText(StartAct.this"BUTTON FROM Share2", Toast.LENGTH_SHORT).show();  

    44.                }  

    45.            });  

    46.            this.setContentView(v);  

    47.        } catch (NameNotFoundException e) {  

    48.            // TODO Auto-generated catch block  

    49.            e.printStackTrace();  

    50.        } catch (ClassNotFoundException e) {  

    51.            // TODO Auto-generated catch block  

    52.            e.printStackTrace();  

    53.        } catch (IllegalArgumentException e) {  

    54.            // TODO Auto-generated catch block  

    55.            e.printStackTrace();  

    56.        } catch (IllegalAccessException e) {  

    57.            // TODO Auto-generated catch block  

    58.            e.printStackTrace();  

    59.        }  

    60.    }  

    61. }  


    资源apk包Layout

    1. <?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_width="fill_parent"  

    5.    android:layout_height="fill_parent"  

    6.    >  

    7. <TextView    

    8.    android:layout_width="fill_parent"  

    9.    android:layout_height="wrap_content"  

    10.    android:text="Share2"  

    11.    />  

    12. <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  

    13. </LinearLayout>  

  • 相关阅读:
    IE11开发人员工具 js脚本debugger调试
    Dynamics CRM OData方式进行增删改查时报错的问题
    Get Form type using javascript in CRM 2011
    Dynamics CRM 同一实体多个Form显示不同的Ribbon按钮
    Dynamics CRM 通过OData查询数据URI中包含中文的情况
    打印控件
    spark
    zookeeper集群配置与启动——实战
    javascript学习
    etcd
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2990605.html
Copyright © 2011-2022 走看看