下面列出了怎么用androidx.annotation.StyleRes的API类实例代码及写法,或者点击链接到github查看源代码。
protected void init(
ComponentContext c,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
Component component) {
mResourceResolver = c.getResourceResolver();
mComponent = component;
mContext = c;
final Component owner = getOwner();
if (owner != null) {
mComponent.mOwnerGlobalKey = owner.getGlobalKey();
}
if (defStyleAttr != 0 || defStyleRes != 0) {
mComponent.getOrCreateCommonProps().setStyle(defStyleAttr, defStyleRes);
component.loadStyle(c, defStyleAttr, defStyleRes);
}
mComponent.setBuilderContext(c.getAndroidContext());
}
private DatePickerDialog(@NonNull Context context, @StyleRes int themeResId,
@Nullable OnDateSetListener listener, @Nullable Calendar calendar, int year,
int monthOfYear, int dayOfMonth) {
super(context, resolveDialogTheme(context, themeResId));
final Context themeContext = getContext();
final LayoutInflater inflater = LayoutInflater.from(themeContext);
final View view = inflater.inflate(R.layout.date_picker_dialog, null);
setView(view);
setButton(BUTTON_POSITIVE, themeContext.getString(android.R.string.ok), this);
setButton(BUTTON_NEGATIVE, themeContext.getString(android.R.string.cancel), this);
// FIXME ? setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
if (calendar != null) {
year = calendar.get(Calendar.YEAR);
monthOfYear = calendar.get(Calendar.MONTH);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
}
mDatePicker = view.findViewById(R.id.datePicker);
mDatePicker.init(year, monthOfYear, dayOfMonth, this);
mDatePicker.setValidationCallback(mValidationCallback);
mDateSetListener = listener;
}
static void applyStyles(InternalNode node, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
if (defStyleAttr != 0 || defStyleRes != 0) {
ComponentContext c = node.getContext();
// TODO: (T55170222) Pass the styles through the InternalNode instead of mutating the context.
c.setDefStyle(defStyleAttr, defStyleRes);
final TypedArray typedArray =
c.getAndroidContext()
.obtainStyledAttributes(null, R.styleable.ComponentLayout, defStyleAttr, defStyleRes);
node.applyAttributes(typedArray);
typedArray.recycle();
// TODO: (T55170222) Not required if styles are passed through the InternalNode.
c.setDefStyle(0, 0);
}
}
public static @StyleRes
int themeNormalFromId(String themeId) {
switch (themeId) {
case "light;pink":
return R.style.AppTheme_Light_Pink;
case "light;cream":
return R.style.AppTheme_Light_Cream;
case "light;green":
return R.style.AppTheme_Light_Green;
case "dark;pink":
return R.style.AppTheme_Dark_Pink;
case "dark;green":
return R.style.AppTheme_Dark_Green;
case "dark;cream":
return R.style.AppTheme_Dark_Cream;
case "amoled;pink":
return R.style.AppTheme_AMOLED_Pink;
case "amoled;green":
return R.style.AppTheme_AMOLED_Green;
case "amoled;cream":
return R.style.AppTheme_AMOLED_Cream;
}
return R.style.AppTheme_Light_Pink;
}
public static @StyleRes
int themeNoActionBarFromId(String themeId) {
switch (themeId) {
case "light;pink":
return R.style.AppTheme_Light_Pink_NoActionBar;
case "light;green":
return R.style.AppTheme_Light_Green_NoActionBar;
case "light;cream":
return R.style.AppTheme_Light_Cream_NoActionBar;
case "dark;pink":
return R.style.AppTheme_Dark_Pink_NoActionBar;
case "dark;green":
return R.style.AppTheme_Dark_Green_NoActionBar;
case "dark;cream":
return R.style.AppTheme_Dark_Cream_NoActionBar;
case "amoled;pink":
return R.style.AppTheme_AMOLED_Pink_NoActionBar;
case "amoled;green":
return R.style.AppTheme_AMOLED_Green_NoActionBar;
case "amoled;cream":
return R.style.AppTheme_AMOLED_Cream_NoActionBar;
}
return R.style.AppTheme_Light_Pink_NoActionBar;
}
public static @StyleRes
int themeTransparentStatusBarFromId(String themeId) {
switch (themeId) {
case "light;pink":
return R.style.AppTheme_Light_Pink_TransparentStatusBar;
case "light;green":
return R.style.AppTheme_Light_Green_TransparentStatusBar;
case "light;cream":
return R.style.AppTheme_Light_Cream_TransparentStatusBar;
case "dark;pink":
return R.style.AppTheme_Dark_Pink_TransparentStatusBar;
case "dark;green":
return R.style.AppTheme_Dark_Green_TransparentStatusBar;
case "dark;cream":
return R.style.AppTheme_Dark_Cream_TransparentStatusBar;
case "amoled;pink":
return R.style.AppTheme_AMOLED_Pink_TransparentStatusBar;
case "amoled;green":
return R.style.AppTheme_AMOLED_Green_TransparentStatusBar;
case "amoled;cream":
return R.style.AppTheme_AMOLED_Cream_TransparentStatusBar;
}
return R.style.AppTheme_Light_Pink_TransparentStatusBar;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
attrHandler(context, attrs, defStyleAttr, defStyleRes);
/*final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CalendarView, defStyleAttr, defStyleRes);
final int mode = a.getInt(R.styleable.CalendarView_calendarViewMode, MODE_HOLO);
a.recycle();
switch (mode) {
case MODE_HOLO:
mDelegate = new CalendarViewLegacyDelegate(
this, context, attrs, defStyleAttr, defStyleRes);
break;
case MODE_MATERIAL:
mDelegate = new CalendarViewMaterialDelegate(
this, context, attrs, defStyleAttr, defStyleRes);
break;
default:
throw new IllegalArgumentException("invalid calendarViewMode attribute");
}*/
}
public static Builder create(
ComponentContext context,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
boolean callsShouldUpdateOnMount,
boolean isPureRender,
boolean canMeasure,
boolean implementsAccessibility) {
return create(
context,
defStyleAttr,
defStyleRes,
callsShouldUpdateOnMount,
isPureRender,
canMeasure,
implementsAccessibility,
false);
}
/**
* Initialize colors from the supplied local dynamic app theme.
*
* @param localTheme The local theme resource to initialize colors.
* @param dynamicLocalTheme The local dynamic app theme to initialize colors.
*
* @return The {@link DynamicTheme} object to allow for chaining of calls to set methods.
*/
public @NonNull DynamicTheme setLocalTheme(@StyleRes int localTheme,
@Nullable DynamicAppTheme dynamicLocalTheme) {
if (dynamicLocalTheme != null) {
if (dynamicLocalTheme.getThemeRes() == DynamicResourceUtils.ADS_DEFAULT_RESOURCE_ID) {
throw new IllegalStateException("Dynamic app theme style resource " +
"id is not found for the application theme.");
}
setLocalThemeRes(dynamicLocalTheme.getThemeRes(), dynamicLocalTheme);
} else {
setLocalThemeRes(localTheme, null);
}
return this;
}
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 getAttributeSet(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
if (attrs != null) {
try {
TypedArray attributes = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.StarkSpinner, defStyleAttr, defStyleRes);
mRevealViewBackgroundColor = attributes.getColor(R.styleable.StarkSpinner_RevealViewBackgroundColor, Color.WHITE);
mStartEditTintColor = attributes.getColor(R.styleable.StarkSpinner_StartSearchTintColor, Color.GRAY);
mEditViewBackgroundColor = attributes.getColor(R.styleable.StarkSpinner_SearchViewBackgroundColor, Color.WHITE);
mEditViewTextColor = attributes.getColor(R.styleable.StarkSpinner_SearchViewTextColor, Color.BLACK);
mDoneEditTintColor = attributes.getColor(R.styleable.StarkSpinner_DoneSearchTintColor, Color.GRAY);
mBordersSize = attributes.getDimensionPixelSize(R.styleable.StarkSpinner_BordersSize, 4);
mExpandSize = attributes.getDimensionPixelSize(R.styleable.StarkSpinner_SpinnerExpandHeight, 0);
mShowBorders = attributes.getBoolean(R.styleable.StarkSpinner_ShowBorders, false);
mBoarderColor = attributes.getColor(R.styleable.StarkSpinner_BoarderColor, Color.GRAY);
mAnimDuration = attributes.getColor(R.styleable.StarkSpinner_AnimDuration, DefaultAnimationDuration);
mKeepLastSearch = attributes.getBoolean(R.styleable.StarkSpinner_KeepLastSearch, false);
mRevealEmptyText = attributes.getString(R.styleable.StarkSpinner_RevealEmptyText);
mSearchHintText = attributes.getString(R.styleable.StarkSpinner_SearchHintText);
mNoItemsFoundText = attributes.getString(R.styleable.StarkSpinner_NoItemsFoundText);
mListItemDivider = attributes.getDrawable(R.styleable.StarkSpinner_ItemsDivider);
mListDividerSize = attributes.getDimensionPixelSize(R.styleable.StarkSpinner_DividerHeight, 0);
} catch (UnsupportedOperationException e) {
Log.e("SearchableSpinner", "getAttributeSet --> " + e.getLocalizedMessage());
}
}
}
private @StyleRes int getSelectedTheme(Activity activity) {
if (isDarkTheme(activity)) {
return getDarkThemeStyle();
} else {
return getLightThemeStyle();
}
}
private void loadErrorTextColorResFromAttributes(@StyleRes int resId) {
if (resId != INVALID_ID) {
TypedArray errorTA = getContext().obtainStyledAttributes(resId, skin.support.R.styleable.SkinTextAppearance);
if (errorTA.hasValue(skin.support.R.styleable.SkinTextAppearance_android_textColor)) {
mErrorTextColorResId = errorTA.getResourceId(skin.support.R.styleable.SkinTextAppearance_android_textColor, INVALID_ID);
}
errorTA.recycle();
}
applyErrorTextColorResource();
}
private static Builder newBuilder(
ComponentContext context,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
TestLayoutComponent state) {
final Builder builder = new Builder();
builder.init(context, defStyleAttr, defStyleRes, state);
return builder;
}
public static Builder create(
ComponentContext context,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
boolean callsShouldUpdateOnMount,
boolean isPureRender,
boolean hasMountSpecChild,
boolean isDelegate) {
return newBuilder(
context,
defStyleAttr,
defStyleRes,
new TestLayoutComponent(
callsShouldUpdateOnMount, isPureRender, hasMountSpecChild, isDelegate));
}
@StyleRes
public static int getTheme(@NonNull Context context) {
@StyleRes int theme = BPrefs.Themes.THEMES[context.getSharedPreferences(D.BALD_PREFS, Context.MODE_PRIVATE).getInt(BPrefs.THEME_KEY, BPrefs.THEME_DEFAULT_VALUE)];
if (theme == -1) {
int hour = DateTime.now().getHourOfDay();
if (hour > 6 && hour < 19)
return BPrefs.Themes.THEMES[BPrefs.Themes.LIGHT];
return BPrefs.Themes.THEMES[BPrefs.Themes.DARK];
}
return theme;
}
private void attrHandler(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CalendarView, defStyleAttr, defStyleRes);
a.recycle();
mDelegate = new CalendarViewMaterialDelegate(this, context, attrs, defStyleAttr, defStyleRes);
}
private void init(
ComponentContext context,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
TestDrawableComponent component) {
super.init(context, defStyleAttr, defStyleRes, component);
mComponent = component;
}
public CustomProfilesAdapter(Context context, List<MultiProfile> profiles, @StyleRes int overrideStyle, DrawerManager.ProfilesDrawerListener<MultiProfile> listener) {
super(context, profiles, listener);
if (overrideStyle == 0) this.inflater = LayoutInflater.from(context);
else this.inflater = LayoutInflater.from(new ContextThemeWrapper(context, overrideStyle));
forceWhite = overrideStyle == R.style.ForceWhite;
}
/**
* 设置动画,已经封装好几种样式,具体可见{@link AnimAction}类
*/
public B setAnimStyle(@StyleRes int id) {
mAnimations = id;
if (isCreated()) {
mPopupWindow.setAnimationStyle(id);
}
return (B) this;
}
/**
* 设置动画,已经封装好几种样式,具体可见{@link AnimAction}类
*/
public B setAnimStyle(@StyleRes int id) {
mAnimations = id;
if (isCreated()) {
mDialog.setWindowAnimations(id);
}
return (B) this;
}
@Override
public void setItemTextAppearance(@StyleRes int resId) {
super.setItemTextAppearance(resId);
if (resId != INVALID_ID) {
TypedArray a = getContext().obtainStyledAttributes(resId, R.styleable.SkinTextAppearance);
if (a.hasValue(R.styleable.SkinTextAppearance_android_textColor)) {
mTextColorResId = a.getResourceId(R.styleable.SkinTextAppearance_android_textColor, INVALID_ID);
}
a.recycle();
applyItemTextColorResource();
}
}
static int resolveDialogTheme(@NonNull Context context, @StyleRes int resid) {
// Check to see if this resourceId has a valid package ID.
if (((resid >>> 24) & 0x000000ff) >= 0x00000001) { // start of real resource IDs.
return resid;
} else {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
return outValue.resourceId;
}
}
/**
* 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();
}
}
private void init(
ComponentContext context,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
TestTransitionComponent state) {
super.init(context, defStyleAttr, defStyleRes, state);
mState = state;
}
/**
* Extract the supplied dimension attribute value from the theme.
*
* @param theme The theme to get the styled attributes.
* @param attr The dimension 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 float resolveDimension(@NonNull Context context,
@StyleRes int theme, @AttrRes int attr, float defaultValue) {
TypedArray a = context.getTheme().obtainStyledAttributes(theme, new int[] { attr });
try {
return a.getDimension(0, defaultValue);
} catch (Exception e) {
return defaultValue;
} finally {
a.recycle();
}
}
/**
* Extract the supplied dimension attribute value from the theme.
* <p>The extracted value will be converted into the integer pixels.
*
* @param theme The theme to get the styled attributes.
* @param attr The dimension attribute whose value to 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 resolveDimensionPixelOffSet(@NonNull Context context,
@StyleRes int theme, @AttrRes int attr, int defaultValue) {
TypedArray a = context.getTheme().obtainStyledAttributes(theme, new int[] { attr });
try {
return a.getDimensionPixelOffset(0, defaultValue);
} catch (Exception e) {
return defaultValue;
} finally {
a.recycle();
}
}
private void configureDarkThemeRes(@NonNull String darkThemeResource, @NonNull Context context) {
requireNotNullNotEmpty(darkThemeResource, "darkThemeResource");
checkNotNull(context);
@StyleRes int darkThemeId = getStyleResourceId(darkThemeResource, context);
if (darkThemeId != 0) {
configuration.themeDark(darkThemeId);
}
}
static @StyleRes
int resolveDialogTheme(@NonNull Context context, @StyleRes int themeResId) {
if (themeResId == 0) {
final TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(R.attr.datePickerDialogTheme, outValue, true)) {
return outValue.resourceId;
} else {
return R.style.ThemeOverlay_Material_Dialog_DatePicker;
}
} else {
return themeResId;
}
}
protected @StyleRes int getLightThemeStyle() {
return R.style.TextSecure_LightTheme;
}