下面列出了androidx.annotation.DimenRes#androidx.annotation.AttrRes 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public StarkSpinner(@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_stark_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 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);
}
}
private void presentCategoryLabel(@AttrRes int iconAttr) {
switch (iconAttr) {
case R.attr.emoji_category_recent:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__recently_used));
break;
case R.attr.emoji_category_people:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__smileys_and_people));
break;
case R.attr.emoji_category_nature:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__nature));
break;
case R.attr.emoji_category_foods:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__food));
break;
case R.attr.emoji_category_activity:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__activities));
break;
case R.attr.emoji_category_places:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__places));
break;
case R.attr.emoji_category_objects:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__objects));
break;
case R.attr.emoji_category_symbols:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__symbols));
break;
case R.attr.emoji_category_flags:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__flags));
break;
case R.attr.emoji_category_emoticons:
categoryLabel.setText(getString(R.string.ReactWithAnyEmojiBottomSheetDialogFragment__emoticons));
break;
default:
throw new AssertionError();
}
}
public static boolean getThemedBoolean(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.data != 0;
}
return false;
}
public static @ColorInt int getThemedColor(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.data;
}
return Color.RED;
}
public static @Nullable Drawable getThemedDrawable(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return AppCompatResources.getDrawable(context, typedValue.resourceId);
}
return null;
}
/**
* Get the value resource id of a given attribute.
*
* @param context The context to retrieve resources.
* @param attrRes The resource id of the attribute.
*
* @return The value resource id of the supplied attribute.
*/
public static int getResourceIdFromAttributes(@NonNull Context context,
@NonNull AttributeSet attrs, @AttrRes int attrRes) {
TypedArray a = context.obtainStyledAttributes(attrs, new int[] { attrRes });
try {
return a.getResourceId(0, ADS_DEFAULT_RESOURCE_VALUE);
} 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 FixOnItemTouchListenerRecyclerView(@NonNull Context context,
@Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* Queries the theme of the given {@code context} for a theme color.
*
* @param context the context holding the current theme.
* @param attrResId the theme color attribute to resolve.
* @return the theme color
*/
@ColorInt
public static int getThemeColor(@NonNull Context context, @AttrRes int attrResId) {
TypedArray a = context.obtainStyledAttributes(null, new int[]{attrResId});
try {
return a.getColor(0, Color.MAGENTA);
} finally {
a.recycle();
}
}
/**
* Finds and returns an attribute color. If it was not found the method returns the default color
*/
public static int getColorAttribute(@AttrRes int attribute, @ColorRes int defaultColor, Context context) {
TypedValue a = new TypedValue();
context.getTheme().resolveAttribute(attribute, a, true);
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
return a.data;
} else {
return ContextCompat.getColor(context, defaultColor);
}
}
/**
* Extract the supplied integer attribute value from the theme.
*
* @param theme The theme to get the styled attributes.
* @param attr The integer attribute whose value should be extracted.
* @param defaultValue The value to return if the attribute is not defined or not a resource.
*
* @return The value of the supplied attribute.
*/
public static int resolveInteger(@NonNull Context context,
@StyleRes int theme, @AttrRes int attr, int defaultValue) {
TypedArray a = context.getTheme().obtainStyledAttributes(theme, new int[] { attr });
try {
return a.getInteger(0, defaultValue);
} catch (Exception e) {
return defaultValue;
} finally {
a.recycle();
}
}
@ColorInt
public static int resolveAttrAsColor(@NonNull Context context, @AttrRes int id) {
TypedArray a = context.obtainStyledAttributes(new int[]{id});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
/**
* Procedure sets AppCompatImageButton color given by attribute ID
*/
private static void setImageButtonColorAttr(Context context, AppCompatImageButton b, @AttrRes int resId)
{
final int c = getThemeColorAttr(context, resId);
b.clearColorFilter();
b.setColorFilter(c, PorterDuff.Mode.SRC_ATOP);
}
public static void setButtonEnabled(Context context, View b, boolean isEnabled)
{
@AttrRes int resId = isEnabled ? R.attr.colorButtonEnabled : R.attr.colorButtonDisabled;
b.setEnabled(isEnabled);
if (b instanceof AppCompatImageButton)
{
Utils.setImageButtonColorAttr(context, (AppCompatImageButton) b, resId);
}
if (b instanceof AppCompatButton)
{
((AppCompatButton) b).setTextColor(Utils.getThemeColorAttr(context, resId));
}
}
public static void setButtonSelected(Context context, View b, boolean isSelected)
{
@AttrRes int resId = isSelected ? R.attr.colorAccent : R.attr.colorButtonEnabled;
b.setSelected(isSelected);
if (b instanceof AppCompatImageButton)
{
Utils.setImageButtonColorAttr(context, (AppCompatImageButton) b, resId);
}
if (b instanceof AppCompatButton)
{
((AppCompatButton) b).setTextColor(Utils.getThemeColorAttr(context, resId));
}
}
/**
* Procedure sets ImageView background color given by attribute ID
*/
public static void setImageViewColorAttr(Context context, ImageView b, @AttrRes int resId)
{
final int c = getThemeColorAttr(context, resId);
b.clearColorFilter();
b.setColorFilter(c, PorterDuff.Mode.SRC_ATOP);
}
/**
* Fix dialog icon color after dialog creation. Necessary for older Android Versions
*/
public static void fixIconColor(@NonNull AlertDialog dialog, @AttrRes int resId)
{
final ImageView imageView = dialog.findViewById(android.R.id.icon);
if (imageView != null)
{
Utils.setImageViewColorAttr(dialog.getContext(), imageView, resId);
}
}
@AttrRes int getCategoryIconAttr(int position) {
return repository.getEmojiPageModels().get(position).getIconAttr();
}
public DynamicTextView(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
loadFromAttributes(attrs);
}
public DynamicPopupBackground(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DynamicNestedGridView(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public ThemeStore primaryColorDarkAttr(@AttrRes int colorAttr) {
return primaryColorDark(ATHUtil.resolveColor(mContext, colorAttr));
}
public DynamicRadioButton(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
loadFromAttributes(attrs);
}
public static int getDrawableRes(Theme theme, @AttrRes int attr) {
final TypedValue out = new TypedValue();
theme.resolveAttribute(attr, out, true);
return out.resourceId;
}
public ThemePreview(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DynamicLinearLayout(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
loadFromAttributes(attrs);
}
public SearchView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DarkSearchView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
EditText searchText = findViewById(androidx.appcompat.R.id.search_src_text);
searchText.setTextColor(ThemeUtil.getThemedColor(context, R.attr.conversation_subtitle_color));
}