下面列出了androidx.annotation.StyleableRes#androidx.appcompat.widget.TintTypedArray 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setCollapsedTextAppearance(int resId) {
TintTypedArray a = TintTypedArray.obtainStyledAttributes(mView.getContext(), resId, R.styleable.CollapsingTextAppearance);
if (a.hasValue(R.styleable.CollapsingTextAppearance_android_textColor)) {
mCollapsedTextColor = a.getColorStateList(R.styleable.CollapsingTextAppearance_android_textColor);
}
if (a.hasValue(R.styleable.CollapsingTextAppearance_android_textSize)) {
mCollapsedTextSize = a.getDimensionPixelSize(R.styleable.CollapsingTextAppearance_android_textSize,
(int) mCollapsedTextSize);
}
mCollapsedShadowColor = a.getInt(R.styleable.CollapsingTextAppearance_android_shadowColor, 0);
mCollapsedShadowDx = a.getFloat(R.styleable.CollapsingTextAppearance_android_shadowDx, 0);
mCollapsedShadowDy = a.getFloat(R.styleable.CollapsingTextAppearance_android_shadowDy, 0);
mCollapsedShadowRadius = a.getFloat(R.styleable.CollapsingTextAppearance_android_shadowRadius, 0);
a.recycle();
if (Build.VERSION.SDK_INT >= 16) {
mCollapsedTypeface = readFontFamilyTypeface(resId);
}
recalculate();
}
/**
* 设置最大化状态Text样式id
*
* @param resId
*/
public void setExpandedTextAppearance(int resId) {
TintTypedArray a = TintTypedArray.obtainStyledAttributes(mView.getContext(), resId, R.styleable.CollapsingTextAppearance);
if (a.hasValue(R.styleable.CollapsingTextAppearance_android_textColor)) {
mExpandedTextColor = a.getColorStateList(R.styleable.CollapsingTextAppearance_android_textColor);
}
if (a.hasValue(R.styleable.CollapsingTextAppearance_android_textSize)) {
mExpandedTextSize = a.getDimensionPixelSize(R.styleable.CollapsingTextAppearance_android_textSize,
(int) mExpandedTextSize);
}
mExpandedShadowColor = a.getInt(
R.styleable.CollapsingTextAppearance_android_shadowColor, 0);
mExpandedShadowDx = a.getFloat(
R.styleable.CollapsingTextAppearance_android_shadowDx, 0);
mExpandedShadowDy = a.getFloat(
R.styleable.CollapsingTextAppearance_android_shadowDy, 0);
mExpandedShadowRadius = a.getFloat(
R.styleable.CollapsingTextAppearance_android_shadowRadius, 0);
a.recycle();
if (Build.VERSION.SDK_INT >= 16) {
mExpandedTypeface = readFontFamilyTypeface(resId);
}
recalculate();
}
/**
* Returns the {@link ColorStateList} from the given {@link TintTypedArray} attributes. The
* resource can include themeable attributes, regardless of API level.
*/
@Nullable
public static ColorStateList getColorStateList(
@NonNull Context context, @NonNull TintTypedArray attributes, @StyleableRes int index) {
if (attributes.hasValue(index)) {
int resourceId = attributes.getResourceId(index, 0);
if (resourceId != 0) {
ColorStateList value = AppCompatResources.getColorStateList(context, resourceId);
if (value != null) {
return value;
}
}
}
// Reading a single color with getColorStateList() on API 15 and below doesn't always correctly
// read the value. Instead we'll first try to read the color directly here.
if (VERSION.SDK_INT <= VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
int color = attributes.getColor(index, -1);
if (color != -1) {
return ColorStateList.valueOf(color);
}
}
return attributes.getColorStateList(index);
}
/**
* Creates a {@link MaterialShapeDrawable} to use as the {@code itemBackground} and wraps it in an
* {@link InsetDrawable} for margins.
*
* @param a The TintTypedArray containing the resolved NavigationView style attributes.
*/
@NonNull
private final Drawable createDefaultItemBackground(@NonNull TintTypedArray a) {
int shapeAppearanceResId = a.getResourceId(R.styleable.NavigationView_itemShapeAppearance, 0);
int shapeAppearanceOverlayResId =
a.getResourceId(R.styleable.NavigationView_itemShapeAppearanceOverlay, 0);
MaterialShapeDrawable materialShapeDrawable =
new MaterialShapeDrawable(
ShapeAppearanceModel.builder(
getContext(), shapeAppearanceResId, shapeAppearanceOverlayResId)
.build());
materialShapeDrawable.setFillColor(
MaterialResources.getColorStateList(
getContext(), a, R.styleable.NavigationView_itemShapeFillColor));
int insetLeft = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetStart, 0);
int insetTop = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetTop, 0);
int insetRight = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetEnd, 0);
int insetBottom = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetBottom, 0);
return new InsetDrawable(materialShapeDrawable, insetLeft, insetTop, insetRight, insetBottom);
}
public static int getThemeAttrColor(Context context, int attr) {
TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, null, new int[]{attr});
try {
return a.getColor(0, 0);
} finally {
a.recycle();
}
}
@SuppressLint("RestrictedApi")
private int getThemeAttrColor(Context context, int attr) {
TEMP_ARRAY[0] = attr;
TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, null, TEMP_ARRAY);
try {
return a.getColor(0, 0);
} finally {
a.recycle();
}
}
public BottomNavigationViewInner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TintTypedArray a = ThemeEnforcement.obtainTintedStyledAttributes(context, attrs,
R.styleable.BottomNavigationView,
defStyleAttr, R.style.Widget_Design_BottomNavigationView,
new int[]{R.styleable.BottomNavigationView_itemTextAppearanceInactive,
R.styleable.BottomNavigationView_itemTextAppearanceActive});
// clear if you don't have set item icon tint list
if (!a.hasValue(R.styleable.BottomNavigationView_itemIconTint)) {
clearIconTintColor();
}
a.recycle();
}
public TabItem(Context context, AttributeSet attrs) {
super(context, attrs);
final TintTypedArray a =
TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.TabItem);
text = a.getText(R.styleable.TabItem_android_text);
icon = a.getDrawable(R.styleable.TabItem_android_icon);
customLayout = a.getResourceId(R.styleable.TabItem_android_layout, 0);
a.recycle();
}
private boolean hasShapeAppearance(@NonNull TintTypedArray a) {
return a.hasValue(R.styleable.NavigationView_itemShapeAppearance)
|| a.hasValue(R.styleable.NavigationView_itemShapeAppearanceOverlay);
}
/**
* Safely retrieve styled attribute information in this Context's theme using {@link
* androidx.appcompat.widget.TintTypedArray}, after checking whether the theme is compatible with
* the component's given style.
*
* <p>Set a component's {@link R.attr#enforceMaterialTheme enforceMaterialTheme} attribute to
* <code>true</code> to ensure that the Context's theme inherits from {@link
* R.style#Theme_MaterialComponents Theme.MaterialComponents}. For example, you'll want to do this
* if the component uses a new attribute defined in <code>Theme.MaterialComponents</code> like
* {@link R.attr#colorSecondary colorSecondary}.
*
* <p>New components should prefer to use {@link #obtainStyledAttributes(Context, AttributeSet,
* int[], int, int, int...)}, and use
* {@link com.google.android.material.resources.MaterialResources}
* as a replacement for the functionality in {@link androidx.appcompat.widget.TintTypedArray}.
*
* <p>If {@link R.attr#enforceTextAppearance} attribute is set to <code>true</code> and
* textAppearanceResIndices parameter is specified and has non-negative values, this will also
* check that a valid TextAppearance is set on this component for the text appearance resources
* passed in.
*/
public static TintTypedArray obtainTintedStyledAttributes(
@NonNull Context context,
AttributeSet set,
@NonNull @StyleableRes int[] attrs,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
@StyleableRes int... textAppearanceResIndices) {
// First, check for a compatible theme.
checkCompatibleTheme(context, set, defStyleAttr, defStyleRes);
// Then, check that a textAppearance is set if enforceTextAppearance attribute is true
checkTextAppearance(context, set, attrs, defStyleAttr, defStyleRes, textAppearanceResIndices);
// Then, safely retrieve the styled attribute information.
return TintTypedArray.obtainStyledAttributes(context, set, attrs, defStyleAttr, defStyleRes);
}
@SuppressWarnings("RestrictedApi")
private void init(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Context context = getContext();
TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs,
R.styleable.MaterialProgressBar, defStyleAttr, defStyleRes);
mProgressStyle = a.getInt(R.styleable.MaterialProgressBar_mpb_progressStyle,
PROGRESS_STYLE_CIRCULAR);
boolean setBothDrawables = a.getBoolean(
R.styleable.MaterialProgressBar_mpb_setBothDrawables, false);
boolean useIntrinsicPadding = a.getBoolean(
R.styleable.MaterialProgressBar_mpb_useIntrinsicPadding, true);
boolean showProgressBackground = a.getBoolean(
R.styleable.MaterialProgressBar_mpb_showProgressBackground,
mProgressStyle == PROGRESS_STYLE_HORIZONTAL);
int determinateCircularProgressStyle = a.getInt(
R.styleable.MaterialProgressBar_mpb_determinateCircularProgressStyle,
DETERMINATE_CIRCULAR_PROGRESS_STYLE_NORMAL);
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_progressTint)) {
mProgressTintInfo.mProgressTint = a.getColorStateList(
R.styleable.MaterialProgressBar_mpb_progressTint);
mProgressTintInfo.mHasProgressTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_progressTintMode)) {
mProgressTintInfo.mProgressTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_progressTintMode, -1), null);
mProgressTintInfo.mHasProgressTintMode = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_secondaryProgressTint)) {
mProgressTintInfo.mSecondaryProgressTint = a.getColorStateList(
R.styleable.MaterialProgressBar_mpb_secondaryProgressTint);
mProgressTintInfo.mHasSecondaryProgressTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_secondaryProgressTintMode)) {
mProgressTintInfo.mSecondaryProgressTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_secondaryProgressTintMode, -1), null);
mProgressTintInfo.mHasSecondaryProgressTintMode = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_progressBackgroundTint)) {
mProgressTintInfo.mProgressBackgroundTint = a.getColorStateList(
R.styleable.MaterialProgressBar_mpb_progressBackgroundTint);
mProgressTintInfo.mHasProgressBackgroundTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_progressBackgroundTintMode)) {
mProgressTintInfo.mProgressBackgroundTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_progressBackgroundTintMode, -1), null);
mProgressTintInfo.mHasProgressBackgroundTintMode = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_indeterminateTint)) {
mProgressTintInfo.mIndeterminateTint = a.getColorStateList(
R.styleable.MaterialProgressBar_mpb_indeterminateTint);
mProgressTintInfo.mHasIndeterminateTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_indeterminateTintMode)) {
mProgressTintInfo.mIndeterminateTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_indeterminateTintMode, -1), null);
mProgressTintInfo.mHasIndeterminateTintMode = true;
}
a.recycle();
switch (mProgressStyle) {
case PROGRESS_STYLE_CIRCULAR:
if (isIndeterminate() || setBothDrawables) {
if (!isInEditMode()) {
setIndeterminateDrawable(
new AnimationScaleIndeterminateCircularProgressDrawable(context));
}
}
if (!isIndeterminate() || setBothDrawables) {
setProgressDrawable(new CircularProgressDrawable(
determinateCircularProgressStyle, context));
}
break;
case PROGRESS_STYLE_HORIZONTAL:
if (isIndeterminate() || setBothDrawables) {
if (!isInEditMode()) {
setIndeterminateDrawable(new IndeterminateHorizontalProgressDrawable(
context));
}
}
if (!isIndeterminate() || setBothDrawables) {
setProgressDrawable(new HorizontalProgressDrawable(context));
}
break;
default:
throw new IllegalArgumentException("Unknown progress style: " + mProgressStyle);
}
setUseIntrinsicPadding(useIntrinsicPadding);
setShowProgressBackground(showProgressBackground);
}