下面列出了android.content.res.Resources.Theme#resolveAttribute() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initOverlay(Context context, Shape shape) {
// pressed state
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
mPressedOverlay = new ShapeDrawable(shape);
int overlayColor = Color.parseColor("#aa888888");
if (theme.resolveAttribute(R.attr.cp_badgeOverlayColor, typedValue, true)) {
overlayColor = typedValue.data;
}
Paint paint = mPressedOverlay.getPaint();
paint.setColor(overlayColor);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
IconContextMenuItem item = (IconContextMenuItem) getItem(position);
Resources res = parentActivity.getResources();
if (convertView == null) {
TextView temp = new TextView(context);
AbsListView.LayoutParams param = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT);
temp.setLayoutParams(param);
temp.setPadding((int)toPixel(res, 20), 2, (int)toPixel(res, 20), 2);
temp.setGravity(android.view.Gravity.CENTER_VERTICAL);
Theme th = context.getTheme();
TypedValue tv = new TypedValue();
if (th.resolveAttribute(android.R.attr.textAppearanceLargeInverse, tv, true)) {
temp.setTextAppearance(context, tv.resourceId);
}
temp.setMinHeight(LIST_PREFERED_HEIGHT);
temp.setCompoundDrawablePadding((int)toPixel(res, 14));
convertView = temp;
}
TextView textView = (TextView) convertView;
textView.setTag(item);
textView.setText(item.text);
textView.setTextColor(Color.WHITE);
textView.setTypeface(typeface);
Bitmap bitmap = ((BitmapDrawable) item.image).getBitmap();
Drawable d = new BitmapDrawable(parent.getResources(), Bitmap.createScaledBitmap(bitmap, LIST_PREFERED_HEIGHT, LIST_PREFERED_HEIGHT, true));
textView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
return textView;
}
public static int getDrawableRes(Theme theme, @AttrRes int attr) {
final TypedValue out = new TypedValue();
theme.resolveAttribute(attr, out, true);
return out.resourceId;
}
public static int getDrawableRes(Theme theme, @AttrRes int attr) {
final TypedValue out = new TypedValue();
theme.resolveAttribute(attr, out, true);
return out.resourceId;
}
public static int getDrawableRes(Theme theme, @AttrRes int attr) {
final TypedValue out = new TypedValue();
theme.resolveAttribute(attr, out, true);
return out.resourceId;
}
private static void resolveAttribute(Theme theme, int attrId, TypedValue outValue, boolean resolveRefs) {
if (CustomThemeHelper.resolveAttribute(attrId, outValue)) return;
if (!theme.resolveAttribute(attrId, outValue, resolveRefs)) outValue.type = TypedValue.TYPE_NULL;
}
public MaterialTabHost(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
super.setOverScrollMode(this.OVER_SCROLL_NEVER);
layout = new LinearLayout(context);
this.addView(layout);
// get primary and accent color from AppCompat theme
Theme theme = context.getTheme();
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
primaryColor = typedValue.data;
theme.resolveAttribute(R.attr.colorAccent, typedValue, true);
accentColor = typedValue.data;
iconColor = Color.WHITE;
textColor = Color.WHITE;
// get attributes
if(attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaterialTabHost, 0, 0);
try {
hasIcons = a.getBoolean(R.styleable.MaterialTabHost_hasIcons, false);
} finally {
a.recycle();
}
}
else {
hasIcons = false;
}
this.isInEditMode();
density = this.getResources().getDisplayMetrics().density;
scrollable = false;
isTablet = this.getResources().getBoolean(R.bool.isTablet);
// initialize tabs list
tabs = new LinkedList<MaterialTab>();
tabsWidth = new LinkedList<Integer>();
// set background color
super.setBackgroundColor(primaryColor);
}
/**
*
* @param newTheme
* @param resId
* @return
*/
protected int getColor(Theme newTheme) {
TypedValue typedValue = new TypedValue();
newTheme.resolveAttribute(mAttrResId, typedValue, true);
return typedValue.data;
}
/**
*
* @param newTheme
* @param resId
* @return
*/
protected int getColor(Theme newTheme) {
TypedValue typedValue = new TypedValue();
newTheme.resolveAttribute(mAttrResId, typedValue, true);
return typedValue.data;
}