一.
先说一下一般是动态布局最好,效率高,动态就是java写布局,这是 老外的专长,一般res目录是不能打包的,布局动态写,其余的就是图片什么的了,可以建一个assess文件夹,把图片放里面,打jar是可以打到jar里面的。打成jar一般会看着jar会大这样有的占内存不会引用,大家可以混淆,混淆之后会小一点,混淆也非常简单,大家在sdk安装工具里面找到tool工具里面,有一个工具,上网上搜一下就知道了,这我不想找那个文件了,导出jar也很简单和项目导出差不多看图1
这个图是,导出是切记选中的不是项目名,因为一个项目导成jar好多文件不能用,这个时候选src导出,有assess文件的话
二.下面说第二种方式。通过反射
①
[代码]java代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package com.example.activitylibrary;
import android.content.Context;
/**
* 根据资源的名字获取其ID值
*
*/
public class MResource {
public static int getIdByName(Context context, String className, String name) {
String packageName = context.getPackageName();
Class r = null;
int id = 0;
try {
r = Class.forName(packageName + ".R");
Class[] classes = r.getClasses();
Class desireClass = null;
for (int i = 0; i < classes.length; ++i) {
if (classes[i].getName().split("\\$")[1].equals(className)) {
desireClass = classes[i];
break;
}
}
if (desireClass != null)
id = desireClass.getField(name).getInt(desireClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return id;
}
} |
这样通过拿到R文件。
②
第一个activity
[代码]java代码:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package com.example.activitylibrary;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class LibraryActivity extends Activity implements OnClickListener{
String msg = "我是jar包的Activity";
private ImageButton mImageView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(MResource.getIdByName(getApplication(), "layout", "activity_main"));
TextView mTextView = (TextView) findViewById(MResource.getIdByName(getApplication(), "id", "textView1"));
mTextView.setText(msg);
mButton = (Button) findViewById(MResource.getIdByName(getApplication(), "id", "button1"));
mButton.setText(msg);
mImageView = (ImageButton) findViewById(MResource.getIdByName(getApplication(), "id", "imageButton"));
initDate();
}
private void initDate() {
mImageView.setOnClickListener(this);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int viewId = v.getId();
if(viewId == MResource.getIdByName(getApplication(), "id", "imageButton")){
Intent intent = new Intent(LibraryActivity.this,WStartActivity.class);
startActivity(intent);
return;
}
if(viewId == MResource.getIdByName(getApplication(), "id", "button1")){
Toast.makeText(getApplicationContext(), "你点击了", Toast.LENGTH_SHORT).show();
return;
}
}
} |
③第二个activity
[代码]java代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.example.activitylibrary;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class WStartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(MResource.getIdByName(getApplication(), "layout", "start_activity_main"));
String msg = "我是点击图片跳转的过来的";
TextView mTextView = (TextView) findViewById(MResource.getIdByName(getApplication(), "id", "textView2"));
mTextView.setText(msg);
}
} |
布局
[代码]xml代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true">
<textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/button1">
<imageview android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/textView1" android:layout_margintop="28dp" android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/ic_launcher">
<imagebutton android:id="@+id/imageButton" android:layout_width="35dp" android:layout_height="35dp" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_margintop="5dp" android:layout_marginright="3dp" android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/icon_education_tap">
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<textview android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5dp" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/button1">
</textview></relativelayout></imagebutton></imageview></textview></button></relativelayout> |
[代码]java代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.example.androidlibraryinvoke;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClassName(getApplication(), "com.example.activitylibrary.LibraryActivity");
startActivity(intent);
}
});
}
} |
然后把个这些打成jar
写一个项目把布局和图片不知到对应项目里的文件夹下。
这样调用
这样一切就OK了。注意必须是你在jar包文件里的布局和图片放到你新建项目文件里。
共同学习,写下你的评论
评论加载中...
作者其他优质文章