下面列出了android.content.res.TypedArray#hasValueOrEmpty() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
* @hide-
*/
public PreferenceScreen(Context context, AttributeSet attrs) {
super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
TypedArray a = context.obtainStyledAttributes(null,
com.android.internal.R.styleable.PreferenceScreen,
com.android.internal.R.attr.preferenceScreenStyle,
0);
mLayoutResId = a.getResourceId(
com.android.internal.R.styleable.PreferenceScreen_screenLayout,
mLayoutResId);
if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) {
mDividerDrawable =
a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider);
mDividerSpecified = true;
}
a.recycle();
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TypedArray a = getActivity().obtainStyledAttributes(null,
com.android.internal.R.styleable.PreferenceFragment,
com.android.internal.R.attr.preferenceFragmentStyle,
0);
ListView lv = (ListView) view.findViewById(android.R.id.list);
if (lv != null
&& a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceFragment_divider)) {
lv.setDivider(
a.getDrawable(com.android.internal.R.styleable.PreferenceFragment_divider));
}
a.recycle();
}
public TabWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.TabWidget, defStyleAttr, defStyleRes);
mDrawBottomStrips = a.getBoolean(R.styleable.TabWidget_tabStripEnabled, mDrawBottomStrips);
// Tests the target SDK version, as set in the Manifest. Could not be
// set using styles.xml in a values-v? directory which targets the
// current platform SDK version instead.
final boolean isTargetSdkDonutOrLower =
context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT;
final boolean hasExplicitLeft = a.hasValueOrEmpty(R.styleable.TabWidget_tabStripLeft);
if (hasExplicitLeft) {
mLeftStrip = a.getDrawable(R.styleable.TabWidget_tabStripLeft);
} else if (isTargetSdkDonutOrLower) {
mLeftStrip = context.getDrawable(R.drawable.tab_bottom_left_v4);
} else {
mLeftStrip = context.getDrawable(R.drawable.tab_bottom_left);
}
final boolean hasExplicitRight = a.hasValueOrEmpty(R.styleable.TabWidget_tabStripRight);
if (hasExplicitRight) {
mRightStrip = a.getDrawable(R.styleable.TabWidget_tabStripRight);
} else if (isTargetSdkDonutOrLower) {
mRightStrip = context.getDrawable(R.drawable.tab_bottom_right_v4);
} else {
mRightStrip = context.getDrawable(R.drawable.tab_bottom_right);
}
a.recycle();
setChildrenDrawingOrderEnabled(true);
}
static boolean hasValueOrEmpty(TypedArray ta, int index) {
if (Build.VERSION.SDK_INT >= 22) {
return ta.hasValueOrEmpty(index);
} else {
return ta.hasValue(index);
}
}
public ListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ListView, defStyleAttr, defStyleRes);
final CharSequence[] entries = a.getTextArray(R.styleable.ListView_entries);
if (entries != null) {
setAdapter(new ArrayAdapter<>(context, R.layout.simple_list_item_1, entries));
}
final Drawable d = a.getDrawable(R.styleable.ListView_divider);
if (d != null) {
// Use an implicit divider height which may be explicitly
// overridden by android:dividerHeight further down.
setDivider(d);
}
final Drawable osHeader = a.getDrawable(R.styleable.ListView_overScrollHeader);
if (osHeader != null) {
setOverscrollHeader(osHeader);
}
final Drawable osFooter = a.getDrawable(R.styleable.ListView_overScrollFooter);
if (osFooter != null) {
setOverscrollFooter(osFooter);
}
// Use an explicit divider height, if specified.
if (a.hasValueOrEmpty(R.styleable.ListView_dividerHeight)) {
final int dividerHeight = a.getDimensionPixelSize(
R.styleable.ListView_dividerHeight, 0);
if (dividerHeight != 0) {
setDividerHeight(dividerHeight);
}
}
mHeaderDividersEnabled = a.getBoolean(R.styleable.ListView_headerDividersEnabled, true);
mFooterDividersEnabled = a.getBoolean(R.styleable.ListView_footerDividersEnabled, true);
a.recycle();
}
/**
* <p>Create a new, empty, non focusable popup window of dimension (0,0).</p>
*
* <p>The popup does not provide a background.</p>
*/
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
mContext = context;
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.PopupWindow, defStyleAttr, defStyleRes);
final Drawable bg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
mElevation = a.getDimension(R.styleable.PopupWindow_popupElevation, 0);
mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false);
// Preserve default behavior from Gingerbread. If the animation is
// undefined or explicitly specifies the Gingerbread animation style,
// use a sentinel value.
if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupAnimationStyle)) {
final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, 0);
if (animStyle == R.style.Animation_PopupWindow) {
mAnimationStyle = ANIMATION_STYLE_DEFAULT;
} else {
mAnimationStyle = animStyle;
}
} else {
mAnimationStyle = ANIMATION_STYLE_DEFAULT;
}
final Transition enterTransition = getTransition(a.getResourceId(
R.styleable.PopupWindow_popupEnterTransition, 0));
final Transition exitTransition;
if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupExitTransition)) {
exitTransition = getTransition(a.getResourceId(
R.styleable.PopupWindow_popupExitTransition, 0));
} else {
exitTransition = enterTransition == null ? null : enterTransition.clone();
}
a.recycle();
setEnterTransition(enterTransition);
setExitTransition(exitTransition);
setBackgroundDrawable(bg);
}
public DatePickerCalendarDelegate(DatePicker delegator, Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(delegator, context);
final Locale locale = mCurrentLocale;
mCurrentDate = Calendar.getInstance(locale);
mTempDate = Calendar.getInstance(locale);
mMinDate = Calendar.getInstance(locale);
mMaxDate = Calendar.getInstance(locale);
mMinDate.set(DEFAULT_START_YEAR, Calendar.JANUARY, 1);
mMaxDate.set(DEFAULT_END_YEAR, Calendar.DECEMBER, 31);
final Resources res = mDelegator.getResources();
final TypedArray a = mContext.obtainStyledAttributes(attrs,
R.styleable.DatePicker, defStyleAttr, defStyleRes);
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final int layoutResourceId = a.getResourceId(
R.styleable.DatePicker_internalLayout, R.layout.date_picker_material);
// Set up and attach container.
mContainer = (ViewGroup) inflater.inflate(layoutResourceId, mDelegator, false);
mContainer.setSaveFromParentEnabled(false);
mDelegator.addView(mContainer);
// Set up header views.
final ViewGroup header = mContainer.findViewById(R.id.date_picker_header);
mHeaderYear = header.findViewById(R.id.date_picker_header_year);
mHeaderYear.setOnClickListener(mOnHeaderClickListener);
mHeaderMonthDay = header.findViewById(R.id.date_picker_header_date);
mHeaderMonthDay.setOnClickListener(mOnHeaderClickListener);
// For the sake of backwards compatibility, attempt to extract the text
// color from the header month text appearance. If it's set, we'll let
// that override the "real" header text color.
ColorStateList headerTextColor = null;
@SuppressWarnings("deprecation")
final int monthHeaderTextAppearance = a.getResourceId(
R.styleable.DatePicker_headerMonthTextAppearance, 0);
if (monthHeaderTextAppearance != 0) {
final TypedArray textAppearance = mContext.obtainStyledAttributes(null,
ATTRS_TEXT_COLOR, 0, monthHeaderTextAppearance);
final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0);
headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor);
textAppearance.recycle();
}
if (headerTextColor == null) {
headerTextColor = a.getColorStateList(R.styleable.DatePicker_headerTextColor);
}
if (headerTextColor != null) {
mHeaderYear.setTextColor(headerTextColor);
mHeaderMonthDay.setTextColor(headerTextColor);
}
// Set up header background, if available.
if (a.hasValueOrEmpty(R.styleable.DatePicker_headerBackground)) {
header.setBackground(a.getDrawable(R.styleable.DatePicker_headerBackground));
}
a.recycle();
// Set up picker container.
mAnimator = mContainer.findViewById(R.id.animator);
// Set up day picker view.
mDayPickerView = mAnimator.findViewById(R.id.date_picker_day_picker);
mDayPickerView.setFirstDayOfWeek(mFirstDayOfWeek);
mDayPickerView.setMinDate(mMinDate.getTimeInMillis());
mDayPickerView.setMaxDate(mMaxDate.getTimeInMillis());
mDayPickerView.setDate(mCurrentDate.getTimeInMillis());
mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener);
// Set up year picker view.
mYearPickerView = mAnimator.findViewById(R.id.date_picker_year_picker);
mYearPickerView.setRange(mMinDate, mMaxDate);
mYearPickerView.setYear(mCurrentDate.get(Calendar.YEAR));
mYearPickerView.setOnYearSelectedListener(mOnYearSelectedListener);
// Set up content descriptions.
mSelectDay = res.getString(R.string.select_day);
mSelectYear = res.getString(R.string.select_year);
// Initialize for current locale. This also initializes the date, so no
// need to call onDateChanged.
onLocaleChanged(mCurrentLocale);
setCurrentView(VIEW_MONTH_DAY);
}
static boolean hasValueOrEmpty(TypedArray array, int index) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
return array.hasValueOrEmpty(index);
}
return array.hasValue(index);
}