1
我有一个自定义的android视图类,其中包括将大量文本直接绘制到提供给其onDraw覆盖的画布中。自定义视图中的Android属性
我想要做的是有一个属性,可以设置为“?android:attr/textAppearanceLarge”类似的东西,并选择常规文本设置,无需进一步的样式。
在我的自定义视图的attrs.xml,我有
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView" >
...
<attr name="textAppearance" format="reference" />
...
</declare-styleable>
</resources>
,然后在CustomView.java
final int[] bogus = new int[] { android.R.attr.textColor, android.R.attr.textSize, android.R.attr.typeface, android.R.attr.textStyle, android.R.attr.fontFamily };
final int ap = styledAttributes.getResourceId(com.test.R.styleable.MyView_textAppearance, -1);
final TypedArray textAppearance = ap != -1 ? context.obtainStyledAttributes(ap, bogus) : null;
if (textAppearance != null) {
for (int i = 0; i < textAppearance.getIndexCount(); i++) {
int attr = textAppearance.getIndex(i);
switch (attr) {
case android.R.attr.textColor: textColor = textAppearance.getColor(attr, textColor); break;
case android.R.attr.textSize: textSize = textAppearance.getDimensionPixelSize(attr, textSize); break;
case android.R.attr.typeface: typefaceIndex = textAppearance.getInt(attr, typefaceIndex); break;
case android.R.attr.textStyle: textStyle = textAppearance.getInt(attr, textStyle); break;
case android.R.attr.fontFamily: fontFamily = textAppearance.getString(attr); break;
}
}
textAppearance.recycle();
}
我已经试过了各种的开关变量的变化,对案件常量等,我永远不会得到任何即使远程有用的东西 。
我在这里做错了什么?