下面列出了android.content.Context#getTheme ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
final Resources.Theme theme = context.getTheme();
int[] styles = new int[]{-2000244};
TypedArray a = theme.obtainStyledAttributes(attrs, styles, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case -2002018:
mText = a.getText(attr);
break;
}
}
a.recycle();
if (!TextUtils.isEmpty(mText)) {
setColorText((String) mText);
}
}
public MaterialTextView(
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(wrap(context, attrs, defStyleAttr, defStyleRes), attrs, defStyleAttr);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
if (canApplyTextAppearanceLineHeight(context)) {
final Resources.Theme theme = context.getTheme();
if (!viewAttrsHasLineHeight(context, theme, attrs, defStyleAttr, defStyleRes)) {
int resId = findViewAppearanceResourceId(theme, attrs, defStyleAttr, defStyleRes);
if (resId != -1) {
applyLineHeightFromViewAppearance(theme, resId);
}
}
}
}
@Override
@SuppressLint("RestrictedApi")
public View onCreateView(String name, Context context, AttributeSet attrs) {
final Resources.Theme theme = context.getTheme();
switch (name) {
case "Button":
return new PandroidCompatButton(new ContextThemeWrapper(context, theme), attrs);
case "EditText":
return new PandroidCompatEditText(new ContextThemeWrapper(context, theme), attrs);
case "RadioButton":
return new PandroidCompatEditText(new ContextThemeWrapper(context, theme), attrs);
case "Switch":
return new PandroidCompatSwitch(new ContextThemeWrapper(context, theme), attrs);
case "TextView":
return new PandroidCompatTextView(new ContextThemeWrapper(context, theme), attrs);
case "ToggleButton":
return new ToggleButton(new ContextThemeWrapper(context, theme), attrs);
default:
return null;
}
}
/**
* Calculates the Action Bar height in pixels.
*/
public static int calculateActionBarSize(Context context) {
if (context == null) {
return 0;
}
Resources.Theme curTheme = context.getTheme();
if (curTheme == null) {
return 0;
}
TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE);
if (att == null) {
return 0;
}
float size = att.getDimension(0, 0);
att.recycle();
return (int) size;
}
private void loadAttributes(Context context, AttributeSet attrs) {
TypedValue outValue = new TypedValue();
Resources.Theme theme = context.getTheme();
// use ?attr/colorPrimary as background color
theme.resolveAttribute(R.attr.colorPrimary, outValue, true);
TypedArray a =
getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.FooterLayout, 0, 0);
int containerGravity;
try {
setColor(a.getColor(R.styleable.FooterLayout_ft_color, outValue.data));
animationDuration =
a.getInteger(R.styleable.FooterLayout_ft_anim_duration, DEFAULT_ANIMATION_DURATION);
containerGravity = a.getInteger(R.styleable.FooterLayout_ft_container_gravity, 1);
mFabSize = a.getInteger(R.styleable.FooterLayout_ft_fab_type, DEFAULT_FAB_SIZE);
} finally {
a.recycle();
}
mFabExpandLayout.setGravity(getGravity(containerGravity));
}
private void setAttributes(Context context, AttributeSet attrs){
Resources.Theme theme = context.getTheme();
if (theme != null) {
TypedArray typedArray = theme.obtainStyledAttributes(attrs, R.styleable.TabView, 0, 0);
if (typedArray != null) {
int n = typedArray.getIndexCount();
for (int i = 0; i < n; i++){
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.TabView_src:
int src = typedArray.getResourceId(i, 0);
setIcon(src);
break;
case R.styleable.TabView_text:
CharSequence text = typedArray.getText(attr);
setText(text);
break;
}
}
typedArray.recycle();
}
}
}
private void parseAttributes(final Context context, final AttributeSet attrs) {
final Resources.Theme theme = context.getTheme();
if(theme != null) {
TypedArray a = theme.obtainStyledAttributes(
attrs,
R.styleable.AbstractCalendarView,
0, 0);
try {
// Retrieve the first day of week
//noinspection ConstantConditions
final int firstDay = a.getInt(R.styleable.AbstractCalendarView_firstDayOfWeek, Calendar.MONDAY);
setFirstDayOfWeek(firstDay);
// Retrieve the last day of week
final int lastDay = a.getInt(R.styleable.AbstractCalendarView_firstDayOfWeek, Calendar.SUNDAY);
setLastDayOfWeek(lastDay);
// Retrieve the style for the day views
mDayStyle = a.getInt(R.styleable.AbstractCalendarView_dayStyle, DayStyleFactory.DEFAULT_STYLE);
} finally {
//noinspection ConstantConditions
a.recycle();
}
}
}
public TVShowsAdapter(Context context) {
// Get the default accent color
Resources.Theme theme = context.getTheme();
TypedArray styledAttributes = theme.obtainStyledAttributes(new int[] {
R.attr.colorAccent,
R.attr.colorInProgress,
R.attr.colorFinished
});
themeAccentColor = styledAttributes.getColor(styledAttributes.getIndex(0), getResources().getColor(R.color.default_accent));
inProgressColor = styledAttributes.getColor(styledAttributes.getIndex(1), getResources().getColor(R.color.orange_500));
finishedColor = styledAttributes.getColor(styledAttributes.getIndex(2), getResources().getColor(R.color.light_green_600));
styledAttributes.recycle();
this.hostManager = HostManager.getInstance(context);
// Get the art dimensions
// Use the same dimensions as in the details fragment, so that it hits Picasso's cache when
// the user transitions to that fragment, avoiding another call and imediatelly showing the image
Resources resources = context.getResources();
artWidth = (int)(resources.getDimension(R.dimen.now_playing_poster_width) /
UIUtils.IMAGE_RESIZE_FACTOR);
artHeight = (int)(resources.getDimension(R.dimen.now_playing_poster_height) /
UIUtils.IMAGE_RESIZE_FACTOR);
}
RecyclerAdapterSlotsItem(Context context, List<SlotsTools.SlotsGroup> slots) {
this.context = context;
this.slots = slots;
Resources.Theme theme = context.getTheme();
// Get the primary text color of the theme
TypedValue typedValueDefault = new TypedValue();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValueDefault, true);
TypedArray arrDefault = context.obtainStyledAttributes(typedValueDefault.data, new int[]{android.R.attr.textColorPrimary});
textColorDefault = arrDefault.getColor(0, -1);
arrDefault.recycle();
TypedValue typedValueError = new TypedValue();
theme.resolveAttribute(R.attr.colorError, typedValueError, true);
TypedArray arrError = context.obtainStyledAttributes(typedValueError.data, new int[]{R.attr.colorError});
textColorError = arrError.getColor(0, -1);
arrError.recycle();
}
public static int getColor(Context context, int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(attr, typedValue, true);
int color = typedValue.data;
return color;
}
public static Drawable getThemeDrawable(Context context, int res) {
Theme theme = context.getTheme();
TypedArray a = theme.obtainStyledAttributes(new int[]{res});
Drawable result = a.getDrawable(0);
a.recycle();
return result;
}
@ColorInt
private static int getColorAttr(@NonNull Context context, int attr) {
Resources.Theme theme = context.getTheme();
TypedArray typedArray = theme.obtainStyledAttributes(new int[]{attr});
final int color = typedArray.getColor(0, Color.LTGRAY);
typedArray.recycle();
return color;
}
public static int getThemeColor(Context context, int id) {
Theme theme = context.getTheme();
TypedArray a = theme.obtainStyledAttributes(new int[]{id});
int result = a.getColor(0, DEFAULT_COLOR);
a.recycle();
return result;
}
private int getPrimaryTextColor(Context context) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
TypedArray arr = context.obtainStyledAttributes(typedValue.data, new int[]{
android.R.attr.textColorPrimary});
int color = arr.getColor(0, -1);
arr.recycle();
return color;
}
@ColorInt
private int getListDividerColor(final Context context) {
final TypedValue typedValue = new TypedValue();
final Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.colorDividerLight, typedValue, true);
return typedValue.data;
}
public static int getThemedResource(Context context, int resource) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(resource, typedValue, true);
return typedValue.resourceId;
}
public static int resolveColor(Context context, int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(attr, typedValue, true);
return typedValue.data;
}
protected void applyTargetOptions(Context context) {
shouldTintTarget = target.tintTarget;
shouldDrawShadow = target.drawShadow;
cancelable = target.cancelable;
// We can't clip out portions of a view outline, so if the user specified a transparent
// target, we need to fallback to drawing a jittered shadow approximation
if (shouldDrawShadow && Build.VERSION.SDK_INT >= 21 && !target.transparentTarget) {
outlineProvider = new ViewOutlineProvider() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void getOutline(View view, Outline outline) {
if (outerCircleCenter == null) return;
outline.setOval(
(int) (outerCircleCenter[0] - outerCircleRadius), (int) (outerCircleCenter[1] - outerCircleRadius),
(int) (outerCircleCenter[0] + outerCircleRadius), (int) (outerCircleCenter[1] + outerCircleRadius));
outline.setAlpha(outerCircleAlpha / 255.0f);
if (Build.VERSION.SDK_INT >= 22) {
outline.offset(0, SHADOW_DIM);
}
}
};
setOutlineProvider(outlineProvider);
setElevation(SHADOW_DIM);
}
setLayerType(LAYER_TYPE_HARDWARE, null);
final Resources.Theme theme = context.getTheme();
isDark = UiUtil.themeIntAttr(context, "isLightTheme") == 0;
final Integer outerCircleColor = target.outerCircleColorInt(context);
if (outerCircleColor != null) {
outerCirclePaint.setColor(outerCircleColor);
} else if (theme != null) {
outerCirclePaint.setColor(UiUtil.themeIntAttr(context, "colorPrimary"));
} else {
outerCirclePaint.setColor(Color.WHITE);
}
final Integer targetCircleColor = target.targetCircleColorInt(context);
if (targetCircleColor != null) {
targetCirclePaint.setColor(targetCircleColor);
} else {
targetCirclePaint.setColor(isDark ? Color.BLACK : Color.WHITE);
}
if (target.transparentTarget) {
targetCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
targetCirclePulsePaint.setColor(targetCirclePaint.getColor());
final Integer targetDimColor = target.dimColorInt(context);
if (targetDimColor != null) {
dimColor = UiUtil.setAlpha(targetDimColor, 0.3f);
} else {
dimColor = -1;
}
final Integer titleTextColor = target.titleTextColorInt(context);
if (titleTextColor != null) {
titlePaint.setColor(titleTextColor);
} else {
titlePaint.setColor(isDark ? Color.BLACK : Color.WHITE);
}
final Integer descriptionTextColor = target.descriptionTextColorInt(context);
if (descriptionTextColor != null) {
descriptionPaint.setColor(descriptionTextColor);
} else {
descriptionPaint.setColor(titlePaint.getColor());
}
if (target.titleTypeface != null) {
titlePaint.setTypeface(target.titleTypeface);
}
if (target.descriptionTypeface != null) {
descriptionPaint.setTypeface(target.descriptionTypeface);
}
}
@Override
public void collect(int mode, View view, Context context, AttributeSet attrs) {
Log.d(TAG, String.format("collected %s %s %s", view, context, attrs));
ColorBox box = ColorBox.newInstance();
onBeforeCollect(view,context,attrs,box);
final Resources.Theme theme = context.getTheme();
int systemStyleResId = 0;
Class clz = this.getClass();
// SystemStyleable
OwlSysStyleable systemStyleable = (OwlSysStyleable) clz.getAnnotation(OwlSysStyleable.class);
if ( systemStyleable != null ){
String value = systemStyleable.value();
systemStyleResId = attrs.getAttributeResourceValue(ANDROID_XML, value, 0);
}
// OwlStyleable
Field[] fields = clz.getFields();
for ( Field field : fields ){
OwlStyleable owlStyleable = field.getAnnotation(OwlStyleable.class);
if ( owlStyleable == null ) continue;
Class scopeClz = field.getDeclaringClass();
OwlAttrScope owlAttrScope = (OwlAttrScope) scopeClz.getAnnotation(OwlAttrScope.class);
if ( owlAttrScope == null ) continue;
int scope = owlAttrScope.value();
int[] styleableResId = getStaticFieldIntArraySafely(field);
if ( styleableResId == null ) continue;
TypedArray a = theme.obtainStyledAttributes(attrs, styleableResId, 0, systemStyleResId);
if ( a != null ){
obtainStyle(view, box, scope, a);
a.recycle();
}
}
onAfterCollect(view,context,attrs,box);
insertSkinBox(view, box);
// first refresh
box.refreshSkin(mode, view, true);
}
public static Drawable getThemeDrawable(Context context, int attr) {
Resources.Theme theme = context.getTheme();
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(attr, typedValue, true);
return typedValue.resourceId != 0 ? ContextCompat.getDrawable(context, typedValue.resourceId) : null;
}