您需要覆盖参数的构造函数Spinner
并从AttributeSet
参数中检索条目。
public class MySpinner extends Spinner {
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
final int[] ids = {android.R.attr.entries};
final TypedArray array = context.obtainStyledAttributes(attrs, ids);
final int entriesId = array.getResourceId(0, 0);
if (entriesId > 0) {
final Resources resources = context.getResources();
final String[] entries = resources.getStringArray(entriesId);
if (entries != null) {
// do whatever you want here
}
}
}
}
}