我正在尝试扩展RadioGroup类,我正在尝试复制以下构造方法:RadioGroup(Context context,AttributeSet attr)。
问题是我在以下参数上遇到编译错误:
com.android.internal.R.styleable.RadioGroup
com.android.internal.R.attr.radioButtonStyle
R.styleable.RadioGroup_checkedButton
com.android.internal.R.styleable.RadioGroup_orientation
这是扩展方法:
public AssistekRadioGroupColumns(Context context, AttributeSet attrs) {
super(context, attrs);
Resources res = Resources.getSystem();
// retrieve selected radio button as requested by the user in the
// XML layout file
TypedArray attributes = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.RadioGroup, com.android.internal.R.attr.radioButtonStyle, 0);
int value = attributes.getResourceId(R.styleable.RadioGroup_checkedButton, View.NO_ID);
if (value != View.NO_ID) {
mCheckedId = value;
}
final int index = attributes.getInt(com.android.internal.R.styleable.RadioGroup_orientation, VERTICAL);
setOrientation(index);
attributes.recycle();
init();
}
我如何获得这些资源,以便扩展课程?
可以看到整个班级(4.0)版本here。
我试图通过反射来获取属性,但这不是很有效:
private int getAttribute(Context con) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
//use reflection to get styleable class.
Field[] alFields = null;
int R_ID = 0;
for (Class<?> c : android.R.class.getClasses()) {
if (c.getName().indexOf("styleable") >= 0) {
alFields = Class.forName( con.getPackageName() + ".R$styleable" ).getFields();
}
}
for (Field f : alFields) {
Log.d("field name",f.getName());
if (f.getName().equals("RadioGroup_checkedButton")) {
int[] ret = (int[])f.get(null);
R_ID = ret[0];
}
}
return R_ID;
}
//gets all RadioGroup R,android.internal.styleable.RadioGroup values
private int[] getAttributes(Context con) throws IllegalAccessException, ClassNotFoundException {
//use reflection to get styleable class.
Field[] alFields = null;
ArrayList<Integer> alInts = new ArrayList<Integer>();
int[] ints = null;
int count = 0;
try {
for (Class<?> c : android.R.class.getClasses()) {
if (c.getName().indexOf("styleable") >= 0) {
//Log.d("get Class Name Outer", c.getName());
//use reflection to access the resource class
alFields = Class.forName( con.getPackageName() + ".R$styleable" ).getFields();
}
}
if (alFields != null)
{
//Log.d("field numbers size", String.valueOf(alFields.length));
for (Field field : alFields) {
Class<?> targetType = field.getType();
//Log.d("field type", field.getType().toString());
if (targetType.equals(Integer.TYPE) && targetType.isPrimitive()) {
Object objectValue = (Integer)field.getInt(null);
alInts.add((Integer)objectValue);
count++;
}
ints = new int[count];
for (int i=0;i<alInts.size();i++) {
ints[i] = alInts.get(i);
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ints;
}
答案 0 :(得分:1)
Class clasz = Class.forName("com.android.internal.R$styleable")
Field field = clasz.getDeclaredField("TextAppearance");
field.setAccessible(true);
int[] textAppearanceStyleArr = (int[])field.get(null);
field = clasz.getDeclaredField("TextAppearance_textSize");
field.setAccessible(true);
int textSizeStyle = (Integer)field.get(null);
答案 1 :(得分:0)
每个开发人员使用的android.jar中都没有包com.android.internal。
但是,你可以使用android模拟器或真实设备上的完整framework.jar来获取它们。
但请注意,您将访问的大多数资源或类都需要您的应用程序具有root访问权限。
您可以在这里找到关于如何使用framework.jar进行开发的好教程:http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/