我扩展了类ImageView并添加了一些自定义参数。我使用Context.getTheme().obtainStyledAttributes()方法成功地从我的代码中获得了这些自定义参数。
我需要的是访问ImageView对象的标准参数,比如android:src和android:background。我知道它存在android.R.styleable.*类,我可以使用它来获取这些参数,但这个类已经被弃用(并且不再可见)。我该怎么做才能访问这些android参数?
发布于 2014-03-24 23:56:11
虽然我不确定如何从TypedArray中提取父值,但您可以使用适当的getter来访问它们,例如:
public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.whatever);
try {
// get custom attributes here
} finally {
array.recycle();
}
// parent attributes
final Drawable background = getBackground();
final Drawable src = getDrawable();
// etc.
}
}这并不完全是你想要的,但它可能会有所帮助。
https://stackoverflow.com/questions/22613354
复制相似问题