Android 获取标准布局属性
我扩展了ImageView类并添加了一些自定义参数。使用方法Android 获取标准布局属性,android,xml,layout,parameters,Android,Xml,Layout,Parameters,我扩展了ImageView类并添加了一些自定义参数。使用方法Context.getTheme().obtainStyledAttributes(),我成功地从代码中获取了这些自定义参数 我需要的是访问ImageView对象的标准参数,例如android:src和android:background。我知道它存在一个类android.R.styleable.*,我可以用它来获取这些参数,但是这个类已经被弃用(并且不再可见)。如何访问这些android参数?虽然我不确定如何从类型Darray中提取父
Context.getTheme().obtainStyledAttributes()
,我成功地从代码中获取了这些自定义参数
我需要的是访问ImageView对象的标准参数,例如
android:src
和android:background
。我知道它存在一个类android.R.styleable.*,我可以用它来获取这些参数,但是这个类已经被弃用(并且不再可见)。如何访问这些android参数?虽然我不确定如何从类型Darray
中提取父值,但您可以使用适当的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.
}
}
这不完全是您想要的,但可能会有所帮助。虽然我不确定如何从
类型Darray
中提取父值,但您可以使用适当的获取程序访问它们,例如:
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.
}
}
这不是您想要的,但可能会有所帮助。这是获取自定义属性的代码。我无法检索到标准的……我不太清楚你所说的标准是什么意思。“parent attributes”注释后的代码不是您想要的吗?我正在尝试获取android属性,例如android:src、android:background、android:layout\u width等。这是获取自定义属性的代码。我无法检索到标准的……我不太清楚你所说的标准是什么意思。“parent attributes”注释后的代码不是您想要的吗?我正在尝试获取android属性,例如android:src、android:background、android:layout\u width等。。。