下面列出了怎么用android.support.annotation.AttrRes的API类实例代码及写法,或者点击链接到github查看源代码。
private void setSelectorDrawable(StateListDrawable stateListDrawable, @StyleableRes int solidAttr, @StyleableRes int strokeAttr, @AttrRes int functionId) throws Exception {
if (typedArray.hasValue(solidAttr) || typedArray.hasValue(strokeAttr)) {
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
if (typedArray.hasValue(solidAttr)) {
tmpDrawable.setColor(typedArray.getColor(solidAttr, 0));
}
if (typedArray.hasValue(strokeAttr)) {
int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.background_bl_stroke_width, 0);
int strokeColor = typedArray.getColor(strokeAttr, 0);
float strokeDashWidth = typedArray.getDimension(R.styleable.background_bl_stroke_dashWidth, 0f);
float strokeGap = typedArray.getDimension(R.styleable.background_bl_stroke_dashGap, 0f);
tmpDrawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeGap);
}
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
}
}
private void setSelectorDrawable(TypedArray typedArray, TypedArray buttonTa,
StateListDrawable stateListDrawable, int attr, @AttrRes int functionId) throws Exception {
int color = 0;
Drawable resDrawable = null;
//这里用try catch先判断是否是颜色而不是直接调用getDrawable,为了方便填入的是颜色时可以沿用其他属性,
//否则如果是其他资源会覆盖app:corners_radius等其他shape属性设置的效果
try {
color = buttonTa.getColor(attr, 0);
if (color == 0) {
resDrawable = buttonTa.getDrawable(attr);
}
} catch (Exception e) {
resDrawable = buttonTa.getDrawable(attr);
}
if (resDrawable == null && color != 0) {
GradientDrawable tmpDrawable = DrawableFactory.getDrawable(typedArray);
tmpDrawable.setColor(color);
stateListDrawable.addState(new int[]{functionId}, tmpDrawable);
} else {
stateListDrawable.addState(new int[]{functionId}, resDrawable);
}
}
public QQNaviView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.QQNaviView, defStyleAttr, 0);
mBigIconSrc = ta.getResourceId(R.styleable.QQNaviView_bigIconSrc, R.drawable.big);
mSmallIconSrc = ta.getResourceId(R.styleable.QQNaviView_smallIconSrc, R.drawable.small);
mIconWidth = ta.getDimension(R.styleable.QQNaviView_iconWidth, dp2px(context, 60));
mIconHeight = ta.getDimension(R.styleable.QQNaviView_iconHeight, dp2px(context, 60));
mRange = ta.getFloat(R.styleable.QQNaviView_range, 1);
ta.recycle();
//默认垂直排列
setOrientation(LinearLayout.VERTICAL);
init(context);
}
public ShadowImageView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
setBackgroundColor(getResources().getColor(android.R.color.transparent));
//获取自定义属性值
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShadowImageView);
if (attrs == null) return;
try {
mShadowOffsetX = a.getDimension(R.styleable.ShadowImageView_shadowOffsetX, 0.0f);
mShadowOffsetY = a.getDimension(R.styleable.ShadowImageView_shadowOffsetY, 0.0f);
mShadowRadius = a.getDimension(R.styleable.ShadowImageView_shadowRadius, 0.0f);
mImgId = a.getResourceId(R.styleable.ShadowImageView_src, -1);
} catch (Exception e) {
e.printStackTrace();
} finally {
a.recycle();
}
mImageView = new CircleImageView(context);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setImageResource(mImgId);
addView(mImageView);
int xPadding = (int) (mShadowRadius + Math.abs(mShadowOffsetX));
int yPadding = (int) (mShadowRadius + Math.abs(mShadowOffsetY));
setPadding(xPadding, yPadding, xPadding, yPadding);
}
public SearchableSpinner(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
getAttributeSet(attrs, defStyleAttr, defStyleRes);
final LayoutInflater factory = LayoutInflater.from(context);
factory.inflate(R.layout.view_searchable_spinner, this, true);
mSpinnerListContainer = (LinearLayout) factory.inflate(R.layout.view_list, this, false);
mSpinnerListView = (ListView) mSpinnerListContainer.findViewById(R.id.LstVw_SpinnerListView);
if (mListItemDivider != null) {
mSpinnerListView.setDivider(mListItemDivider);
mSpinnerListView.setDividerHeight(mListDividerSize);
}
mEmptyTextView = (TextView) mSpinnerListContainer.findViewById(R.id.TxtVw_EmptyText);
mSpinnerListView.setEmptyView(mEmptyTextView);
}
private void getAttributeSet(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
if (attrs != null) {
try {
TypedArray attributes = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.SearchableSpinner, defStyleAttr, defStyleRes);
mRevealViewBackgroundColor = attributes.getColor(R.styleable.SearchableSpinner_RevealViewBackgroundColor, Color.WHITE);
mStartEditTintColor = attributes.getColor(R.styleable.SearchableSpinner_StartSearchTintColor, Color.GRAY);
mEditViewBackgroundColor = attributes.getColor(R.styleable.SearchableSpinner_SearchViewBackgroundColor, Color.WHITE);
mEditViewTextColor = attributes.getColor(R.styleable.SearchableSpinner_SearchViewTextColor, Color.BLACK);
mDoneEditTintColor = attributes.getColor(R.styleable.SearchableSpinner_DoneSearchTintColor, Color.GRAY);
mBordersSize = attributes.getDimensionPixelSize(R.styleable.SearchableSpinner_BordersSize, 4);
mExpandSize = attributes.getDimensionPixelSize(R.styleable.SearchableSpinner_SpinnerExpandHeight, 0);
mShowBorders = attributes.getBoolean(R.styleable.SearchableSpinner_ShowBorders, false);
mBoarderColor = attributes.getColor(R.styleable.SearchableSpinner_BoarderColor, Color.GRAY);
mAnimDuration = attributes.getColor(R.styleable.SearchableSpinner_AnimDuration, DefaultAnimationDuration);
mKeepLastSearch = attributes.getBoolean(R.styleable.SearchableSpinner_KeepLastSearch, false);
mRevealEmptyText = attributes.getString(R.styleable.SearchableSpinner_RevealEmptyText);
mSearchHintText = attributes.getString(R.styleable.SearchableSpinner_SearchHintText);
mNoItemsFoundText = attributes.getString(R.styleable.SearchableSpinner_NoItemsFoundText);
mListItemDivider = attributes.getDrawable(R.styleable.SearchableSpinner_ItemsDivider);
mListDividerSize = attributes.getDimensionPixelSize(R.styleable.SearchableSpinner_DividerHeight, 0);
} catch (UnsupportedOperationException e) {
Log.e("SearchableSpinner", "getAttributeSet --> " + e.getLocalizedMessage());
}
}
}
/**
* Sets the background color of the drawable
**/
public static void setDrawableColor(Context context, Drawable drawable, @AttrRes int colorAttrRes) {
int colorRes = getResourceId(context, colorAttrRes);
int color = ContextCompat.getColor(context, colorRes);
if (drawable instanceof ShapeDrawable) {
((ShapeDrawable) drawable).getPaint().setColor(color);
} else if (drawable instanceof GradientDrawable) {
((GradientDrawable) drawable).setColor(color);
} else if (drawable instanceof ColorDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
((ColorDrawable) drawable).setColor(color);
}
} else if (drawable instanceof RotateDrawable) {
setDrawableColor(context, ((RotateDrawable) drawable).getDrawable(), colorAttrRes);
}
}
/**
* Get a color from the attribute theme
*
* @param context theme context
* @param attributeColor the attribute color, ex R.attr.colorPrimary
* @return the color, or {@link Color#TRANSPARENT} if failed to resolve
*/
public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attributeColor) {
int[] attrs = new int[]{attributeColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int color = ta.getColor(0, Color.TRANSPARENT);
ta.recycle();
return color;
}
public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attributeColor) {
int[] attrs = new int[]{attributeColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int color = ta.getColor(0, Color.TRANSPARENT);
ta.recycle();
return color;
}
@ColorInt
public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attr) {
TypedArray a = context.obtainStyledAttributes(null, new int[]{attr});
try {
return a.getColor(0, 0);
} finally {
a.recycle();
}
}
public static ColorStateList getThemeAttrColorStateList(Context context, @AttrRes int attr) {
TEMP_ARRAY[0] = attr;
TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY);
try {
return a.getColorStateList(0);
} finally {
a.recycle();
}
}
public static int getThemeAttrId(Context context, @AttrRes int attr) {
TEMP_ARRAY[0] = attr;
TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY);
try {
return a.getResourceId(0, 0);
} finally {
a.recycle();
}
}
/**
* Get a drawable from the attribute theme
*
* @param context theme context
* @param attributeDrawable the attribute drawable, ex R.attr.selectableItemBackground
* @return the drawable, if it exists in the theme context
*/
@Nullable
public static Drawable getThemeAttrDrawable(@NonNull Context context, @AttrRes int attributeDrawable) {
int[] attrs = new int[]{attributeDrawable};
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawableFromTheme = ta.getDrawable(0);
ta.recycle();
return drawableFromTheme;
}
public static boolean getThemeAttrBoolean(Context context, @AttrRes int attr) {
TEMP_ARRAY[0] = attr;
TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY);
try {
return a.getBoolean(0, false);
} finally {
a.recycle();
}
}
public static int resolveColor(Context context, @AttrRes int attr, int fallback) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
try {
return a.getColor(0, fallback);
} finally {
a.recycle();
}
}
public Captcha(@NonNull final Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Captcha);
drawableId = typedArray.getResourceId(R.styleable.Captcha_src, R.drawable.cat);
progressDrawableId = typedArray.getResourceId(R.styleable.Captcha_progressDrawable, R.drawable.po_seekbar);
thumbDrawableId = typedArray.getResourceId(R.styleable.Captcha_thumbDrawable, R.drawable.thumb);
mMode = typedArray.getInteger(R.styleable.Captcha_mode, MODE_BAR);
maxFailedCount = typedArray.getInteger(R.styleable.Captcha_max_fail_count, 3);
blockSize = typedArray.getDimensionPixelSize(R.styleable.Captcha_blockSize, Utils.dp2px(getContext(), 50));
typedArray.recycle();
init();
}
/**
* Get a dimen from the attribute theme
*
* @param context theme context
* @param attributeDimen the attribute dimen, ex R.attr.actionBarSize
* @return the dimen pixel size, if it exists in the theme context. Otherwise, -1
*/
public static float getThemeAttrDimen(@NonNull Context context, @AttrRes int attributeDimen) {
TypedValue tv = new TypedValue();
int value = -1;
if (context.getTheme().resolveAttribute(attributeDimen, tv, true)) {
value = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
return value;
}
public static int getColor(Context context, @AttrRes int attr, int defaultColor) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
try {
return a.getColor(0, defaultColor);
} finally {
a.recycle();
}
}
public NetworkStateView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NetworkStateView, defStyleAttr, R.style.NetworkStateView_Style);
mLoadingViewId = typedArray.getResourceId(R.styleable.NetworkStateView_loadingView, R.layout.view_loading);
mErrorViewId = typedArray.getResourceId(R.styleable.NetworkStateView_errorView, R.layout.view_network_error);
mErrorImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvErrorImage, NO_ID);
mErrorText = typedArray.getString(R.styleable.NetworkStateView_nsvErrorText);
mNoNetworkViewId = typedArray.getResourceId(R.styleable.NetworkStateView_noNetworkView, R.layout.view_no_network);
mNoNetworkImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvNoNetworkImage, NO_ID);
mNoNetworkText = typedArray.getString(R.styleable.NetworkStateView_nsvNoNetworkText);
mEmptyViewId = typedArray.getResourceId(R.styleable.NetworkStateView_emptyView, R.layout.view_empty);
mEmptyImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvEmptyImage, NO_ID);
mEmptyText = typedArray.getString(R.styleable.NetworkStateView_nsvEmptyText);
mRefreshViewId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvRefreshImage, NO_ID);
mTextColor = typedArray.getColor(R.styleable.NetworkStateView_nsvTextColor, 0x8a000000);
mTextSize = typedArray.getDimensionPixelSize(R.styleable.NetworkStateView_nsvTextSize, UIUtils.dp2px(14));
typedArray.recycle();
mInflater = LayoutInflater.from(context);
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setBackgroundColor(UIUtils.getColor(R.color.white));
}
/**
* Gets the required boolean value from the current context, if possible/available
* @param context The context to use as reference for the boolean
* @param attr Attribute id to resolve
* @param fallback Default value to return if no value is specified in theme
* @return the boolean value from current theme
*/
private static boolean resolveBoolean(Context context, @AttrRes int attr, boolean fallback) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
try {
return a.getBoolean(0, fallback);
} finally {
a.recycle();
}
}
public NetworkStateView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NetworkStateView, defStyleAttr, R.style.NetworkStateView_Style);
mLoadingViewId = typedArray.getResourceId(R.styleable.NetworkStateView_loadingView, R.layout.view_loading);
mErrorViewId = typedArray.getResourceId(R.styleable.NetworkStateView_errorView, R.layout.view_network_error);
mErrorImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvErrorImage, NO_ID);
mErrorText = typedArray.getString(R.styleable.NetworkStateView_nsvErrorText);
mNoNetworkViewId = typedArray.getResourceId(R.styleable.NetworkStateView_noNetworkView, R.layout.view_no_network);
mNoNetworkImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvNoNetworkImage, NO_ID);
mNoNetworkText = typedArray.getString(R.styleable.NetworkStateView_nsvNoNetworkText);
mEmptyViewId = typedArray.getResourceId(R.styleable.NetworkStateView_emptyView, R.layout.view_empty);
mEmptyImageId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvEmptyImage, NO_ID);
mEmptyText = typedArray.getString(R.styleable.NetworkStateView_nsvEmptyText);
mRefreshViewId = typedArray.getResourceId(R.styleable.NetworkStateView_nsvRefreshImage, NO_ID);
mTextColor = typedArray.getColor(R.styleable.NetworkStateView_nsvTextColor, 0x8a000000);
mTextSize = typedArray.getDimensionPixelSize(R.styleable.NetworkStateView_nsvTextSize, UIUtils.dp2px(14));
typedArray.recycle();
mInflater = LayoutInflater.from(context);
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setBackgroundColor(UIUtils.getColor(R.color.white));
}
public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attr) {
TypedArray a = context.obtainStyledAttributes(null, new int[]{attr});
try {
return a.getColor(0, 0);
} finally {
a.recycle();
}
}
public EditorPanel(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int
defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
View root = LayoutInflater.from(context).inflate(R.layout.editor_panel, this);
initView(root);
initEvent();
}
public ProWebViewControls(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context, R.layout.virtual_buttons, this);
zoomIn = view.findViewById(R.id.btn_zoom_in);
zoomOut = view.findViewById(R.id.btn_zoom_out);
goBottom = view.findViewById(R.id.btn_bottom);
goTop = view.findViewById(R.id.btn_top);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProWebViewControls);
try {
int mode = array.getInt(R.styleable.ProWebViewControls_controlMode, 2);
switch (mode) {
case 0:
setMode(ControlMode.MODE_ZOOM);
break;
case 1:
setMode(ControlMode.MODE_NAVIGATION);
break;
case 2:
setMode(ControlMode.MODE_DUAL);
break;
}
timeout = array.getInteger(R.styleable.ProWebViewControls_hideTimeout, 3000);
} finally {
array.recycle();
}
resetThread();
setListeners();
}
/**
* Convenience method for retrieving a themed drawable.
*
* @param context the {@link Context} to resolve the theme attribute against
* @param attr the attribute corresponding to the drawable to resolve
* @return the drawable of the resolved attribute
*/
public static Drawable resolveDrawable(Context context, @AttrRes int attr) {
final TypedArray a;
synchronized (TEMP_ATTR) {
TEMP_ATTR[0] = attr;
a = context.obtainStyledAttributes(TEMP_ATTR);
}
try {
return a.getDrawable(0);
} finally {
a.recycle();
}
}
/**
* Gets the color attribute from the current theme.
*
* @param context the context to get the theme from.
* @param resource the color attribute resource.
* @return the color for the given attribute.
*/
@ColorInt
public static int getColor(@NonNull Context context, @AttrRes int resource) {
TypedArray a = context.obtainStyledAttributes(sTypedValue.data, new int[]{resource});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
protected final int getSystemColor(@AttrRes int attr) {
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{attr});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
public static int resolveResourceId(@NonNull Context context, @AttrRes int attr, int fallback) {
TEMP_ARRAY[0] = attr;
TypedArray ta = context.obtainStyledAttributes(TEMP_ARRAY);
try {
return ta.getResourceId(0, fallback);
} finally {
ta.recycle();
}
}
public ExpandableButtonView(@NonNull Context context,
@Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
initAttrs(attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ExpandableButtonView(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
initAttrs(attrs);
}