下面列出了android.content.res.TypedArray#getType() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String parseCategories(ShortcutService service, AttributeSet attrs) {
final TypedArray sa = service.mContext.getResources().obtainAttributes(attrs,
R.styleable.ShortcutCategories);
try {
if (sa.getType(R.styleable.ShortcutCategories_name) == TypedValue.TYPE_STRING) {
return sa.getNonResourceString(R.styleable.ShortcutCategories_name);
} else {
Log.w(TAG, "android:name for shortcut category must be string literal.");
return null;
}
} finally {
sa.recycle();
}
}
public RadioGroupSetting(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RadioGroupSetting, defStyleAttr, 0);
mLayout = attributes.getResourceId(R.styleable.RadioGroupSetting_layout, R.layout.setting_radio_group);
mDescription = attributes.getString(R.styleable.RadioGroupSetting_description);
mOptions = attributes.getTextArray(R.styleable.RadioGroupSetting_options);
mDescriptions = attributes.getTextArray(R.styleable.RadioGroupSetting_descriptions);
mMargin = attributes.getDimension(R.styleable.RadioGroupSetting_itemMargin, getDefaultMargin());
int id = attributes.getResourceId(R.styleable.RadioGroupSetting_values, 0);
try {
TypedArray array = context.getResources().obtainTypedArray(id);
if (array.getType(0) == TypedValue.TYPE_STRING) {
mValues = getResources().getStringArray(id);
} else if (array.getType(0) == TypedValue.TYPE_INT_HEX ||
array.getType(0) == TypedValue.TYPE_INT_DEC) {
int [] values = getResources().getIntArray(id);
mValues = new Integer[values.length];
for (int i=0; i<values.length; i++) {
mValues[i] = values[i];
}
}
array.recycle();
} catch (Resources.NotFoundException ignored) {
}
attributes.recycle();
initialize(context, attrs, defStyleAttr, mLayout);
}
public ImageRadioGroupSetting(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RadioGroupSetting, defStyleAttr, 0);
mOptions = new ArrayList<>(Arrays.asList(attributes.getTextArray(R.styleable.RadioGroupSetting_options)));
int id = attributes.getResourceId(R.styleable.RadioGroupSetting_values, 0);
TypedArray array = context.getResources().obtainTypedArray(id);
if (array.getType(0) == TypedValue.TYPE_STRING) {
mValues = new ArrayList<>(Arrays.asList(getResources().getStringArray(id)));
} else if (array.getType(0) == TypedValue.TYPE_INT_HEX ||
array.getType(0) == TypedValue.TYPE_INT_DEC) {
int [] values = getResources().getIntArray(id);
mValues = new ArrayList<>(Arrays.asList(new Integer[values.length]));
for (int value : values) {
mValues.add(value);
}
}
array.recycle();
id = attributes.getResourceId(R.styleable.RadioGroupSetting_images, 0);
array = context.getResources().obtainTypedArray(id);
mImages = new Drawable[mOptions.size()];
for (int i = 0; i < mOptions.size(); ++i) {
mImages[i] = array.getDrawable(i);
}
array.recycle();
attributes.recycle();
initialize(context);
}
public static int getType(TypedArray array, int index) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return array.getType(index);
else {
TypedValue value = array.peekValue(index);
return value == null ? TypedValue.TYPE_NULL : value.type;
}
}
public static int getType(TypedArray array, int index) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return array.getType(index);
else {
TypedValue value = array.peekValue(index);
return value == null ? TypedValue.TYPE_NULL : value.type;
}
}
public static int getType(TypedArray array, int index){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return array.getType(index);
else{
TypedValue value = array.peekValue(index);
return value == null ? TypedValue.TYPE_NULL : value.type;
}
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(@NonNull Context c, AttributeSet attrs) {
// Just so we don't build layout params multiple times.
boolean isString;
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.superslim_LayoutManager);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
TypedValue value = new TypedValue();
a.getValue(R.styleable.superslim_LayoutManager_slm_section_sectionManager, value);
isString = value.type == TypedValue.TYPE_STRING;
} else {
isString =
a.getType(R.styleable.superslim_LayoutManager_slm_section_sectionManager)
== TypedValue.TYPE_STRING;
}
String sectionManager = null;
int sectionManagerKind;
if (isString) {
sectionManager = a
.getString(R.styleable.superslim_LayoutManager_slm_section_sectionManager);
if (TextUtils.isEmpty(sectionManager)) {
sectionManagerKind = SECTION_MANAGER_LINEAR;
} else {
sectionManagerKind = SECTION_MANAGER_CUSTOM;
}
} else {
sectionManagerKind = a
.getInt(R.styleable.superslim_LayoutManager_slm_section_sectionManager,
SECTION_MANAGER_LINEAR);
}
a.recycle();
return getSLM(sectionManagerKind, sectionManager).generateLayoutParams(c, attrs);
}
public static int getType(TypedArray array, int index){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return array.getType(index);
else{
TypedValue value = array.peekValue(index);
return value == null ? TypedValue.TYPE_NULL : value.type;
}
}
private static ShortcutInfo parseShortcutAttributes(ShortcutService service,
AttributeSet attrs, String packageName, ComponentName activity,
@UserIdInt int userId, int rank) {
final TypedArray sa = service.mContext.getResources().obtainAttributes(attrs,
R.styleable.Shortcut);
try {
if (sa.getType(R.styleable.Shortcut_shortcutId) != TypedValue.TYPE_STRING) {
Log.w(TAG, "android:shortcutId must be string literal. activity=" + activity);
return null;
}
final String id = sa.getNonResourceString(R.styleable.Shortcut_shortcutId);
final boolean enabled = sa.getBoolean(R.styleable.Shortcut_enabled, true);
final int iconResId = sa.getResourceId(R.styleable.Shortcut_icon, 0);
final int titleResId = sa.getResourceId(R.styleable.Shortcut_shortcutShortLabel, 0);
final int textResId = sa.getResourceId(R.styleable.Shortcut_shortcutLongLabel, 0);
final int disabledMessageResId = sa.getResourceId(
R.styleable.Shortcut_shortcutDisabledMessage, 0);
if (TextUtils.isEmpty(id)) {
Log.w(TAG, "android:shortcutId must be provided. activity=" + activity);
return null;
}
if (titleResId == 0) {
Log.w(TAG, "android:shortcutShortLabel must be provided. activity=" + activity);
return null;
}
return createShortcutFromManifest(
service,
userId,
id,
packageName,
activity,
titleResId,
textResId,
disabledMessageResId,
rank,
iconResId,
enabled);
} finally {
sa.recycle();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public LayoutParams(@NonNull Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.superslim_LayoutManager);
isHeader = a.getBoolean(
R.styleable.superslim_LayoutManager_slm_isHeader,
false);
//noinspection ResourceType
headerDisplay = a.getInt(
R.styleable.superslim_LayoutManager_slm_headerDisplay,
DEFAULT_HEADER_DISPLAY);
mFirstPosition = a.getInt(
R.styleable.superslim_LayoutManager_slm_section_firstPosition,
NO_FIRST_POSITION);
// Header margin types can be dimension or integer (enum).
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
TypedValue value = new TypedValue();
a.getValue(R.styleable.superslim_LayoutManager_slm_section_headerMarginStart,
value);
loadHeaderStartMargin(a, value.type == TypedValue.TYPE_DIMENSION);
a.getValue(R.styleable.superslim_LayoutManager_slm_section_headerMarginEnd, value);
loadHeaderEndMargin(a, value.type == TypedValue.TYPE_DIMENSION);
a.getValue(R.styleable.superslim_LayoutManager_slm_section_sectionManager, value);
loadSlm(a, value.type == TypedValue.TYPE_STRING);
} else {
boolean isDimension;
isDimension =
a.getType(R.styleable.superslim_LayoutManager_slm_section_headerMarginStart)
== TypedValue.TYPE_DIMENSION;
loadHeaderStartMargin(a, isDimension);
isDimension =
a.getType(R.styleable.superslim_LayoutManager_slm_section_headerMarginEnd)
== TypedValue.TYPE_DIMENSION;
loadHeaderEndMargin(a, isDimension);
boolean isString =
a.getType(R.styleable.superslim_LayoutManager_slm_section_sectionManager)
== TypedValue.TYPE_STRING;
loadSlm(a, isString);
}
a.recycle();
}