这几天需要用到一个动态的多选列表框,结果从资源文件中设置是没有问题的。
例1:
<CheckedTextView android:id="@+id/checkedTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:text="@string/loansmanager" />
于是我为了动态生成,在java代码中这样设置
CheckedTextView2=new CheckedTextView(this); CheckedTextView2.setText("test2"); CheckedTextView2.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple); myLayout.addView(CheckedTextView2);
结果报了异常android.content.res.Resources$NotFoundException when programmatically setting android.R.attr.listChoiceIndicatorMultiple·····
理论上这样是没有问题的,可能在绘制时需要先取到id,在绘制吧。google了一下,解决了,关键代码如下
TypedArray ta = getBaseContext().getTheme().obtainStyledAttributes(attrs);
//初始化绘制目标
Drawable indicator = ta.getDrawable(0);
CheckedTextView1=new CheckedTextView(this);
CheckedTextView1.setText("test1");
//得到绘制目标后放入选择框中
CheckedTextView1.setCheckMarkDrawable(indicator);
- (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
-(NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
return platform;
}
获取外部Apk资源的实体类:
public class ForeignPackage {
public final static String TAG = "ForeignPackage";
private String packageName;
private Context foreignCtx;
public ForeignPackage(Context ctx, String packageName) throws NameNotFoundException{
this.packageName = packageName;
foreignCtx = ctx.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
}
//获取该apk的上下文
public Context getContext(){
return foreignCtx;
}
//获取该apk的asset
public AssetManager getAssets(){
return foreignCtx.getAssets();
}
//获取该apk的Class
public Class<?> loadClass(String className) throws ClassNotFoundException{
return foreignCtx.getClassLoader().loadClass(className);
}
//ID: 形如R.layout.main
public int getResourceID(String sID){
int mID = -1;
if(sID.indexOf('.') == -1 || sID.indexOf('.') == sID.length() - 1){
return -1;
}
//String className = "com.apktest.R$attr";
String className = packageName + "." + sID.substring(0, sID.lastIndexOf('.')).replace('.', '$');
String idName = sID.substring(sID.lastIndexOf('.') + 1);
try {
Class<?> c = loadClass(className);
Field field = c.getField(idName);
mID = field.getInt(null);
} catch (ClassNotFoundException e) {
Log.e(TAG, "className Not Found:" + className);
return -1;
} catch (SecurityException e) {
Log.e(TAG, "SecurityException:" + sID);
return -1;
} catch (NoSuchFieldException e) {
Log.e(TAG, "The Field Not Found:" + idName);
return -1;
} catch (Exception e) {
Log.e(TAG, "Exception:" + e);
return -1;
}
return mID;
}
//获取该apk包中的string资源
public String getString(String id){
int mID = getResourceID(id);
if(mID == -1){
return null;
}
String s = null;
try {
s = foreignCtx.getResources().getString(mID);
} catch (Exception e) {
Log.e(TAG, "Exception:" + e);
}
return s;
}
//获取该apk包中的color资源
public int getColor(String id){
int mID = getResourceID(id);
if(mID == -1){
return -1;
}
int color = -1;
try {
color = foreignCtx.getResources().getColor(mID);
} catch (Exception e) {
Log.e(TAG, "Exception:" + e);
}
return color;
}
//获取该apk包中的drawable资源
public Drawable getDrawable(String id){
int mID = getResourceID(id);
if(mID == -1){
return null;
}
Drawable d = null;
try {
d = foreignCtx.getResources().getDrawable(mID);
} catch (Exception e) {
Log.e(TAG, "Exception:" + e);
}
return d;
}
//获取该apk包中的layout资源
public View getLayout(String id){
int mID = getResourceID(id);
if(mID == -1){
return null;
}
LayoutInflater inflate = (LayoutInflater)foreignCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflate.inflate(mID, null);
}
//获取该apk包中的animation资源
public Animation getAnimation(String id){
int mID = getResourceID(id);
if(mID == -1){
return null;
}
Animation a = null;
try {
a = AnimationUtils.loadAnimation(foreignCtx, mID);
} catch (Exception e) {
Log.e(TAG, "Exception:" + e);
}
return a;
}
//获取该apk包中的类实力,className为类名,args为类的构造函数参数
public Object getClassInstance(String className, Object...args) throws ClassNotFoundException{
Class<?> c = loadClass(className);
try {
Class<?>[] cs = new Class<?>[args.length];
int i = 0;
for(Object arg : args){
cs[i++] = InstanceMapping.classTypeFormat(arg.getClass());
}
Constructor<?> ct = c.getConstructor(cs);
return ct.newInstance(args);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}