在需要改变系统默认效果样式的时候,可以通过findViewById的方式获取到系统UI控件。
如:
com.android.internal.R.title
com.android.internal.R.icon
com.android.internal.R.summary
com.android.internal.R.title_container
等一些系统定义的ID值。普通应用(非系统级应用)是无法直接访问到com.android.internal.R.id.*属性的,此时就需要使用反射来实现了。
实现代码如下:
public static int getReflactField(String className,String fieldName){
int result = -1;
try {
Class<?> clz = Class.forName(className);
Field field = clz.getField(fieldName);
field.setAccessible(true);
result = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
调用示例:
int title_id = getReflactField("com.android.internal.R$id", "title");
int icon_id = getReflactField("com.android.internal.R$id", "icon");
此外,Field 这个类定义了一系列的 get 方法来获取不同类型的值。可以利用下面的方法获取
public Object get(Object obj);
public int getInt(Object obj);
public long getLong(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public float getFloat(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public short getShort(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public double getDouble(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public char getChar(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public byte getByte(Object obj)
throws IllegalArgumentException, IllegalAccessException;
public boolean getBoolean(Object obj)
throws IllegalArgumentException, IllegalAccessException

792

被折叠的 条评论
为什么被折叠?



