如何访问多种格式的自定义属性?
how to access custom attribute with multiple formats?
我在另一个答案中读到,在 android 中,您可以为具有多种格式的自定义视图声明属性,如下所示:
<attr name="textColor" format="reference|color"/>
如何在 class 中访问这些属性?我应该假设它是一个参考,使用 getResources().getColorStateList()
然后假设它是原始的 RGB/ARGB 颜色如果 Resources.getColorStateList()
抛出 Resources.NotFoundException
或者有更好的区分方法在 formats/types?
之间
在你的/res/attrs.xml:
<declare-styleable name="YourTheme">
<attr name="textColor" format="reference|color"/>
</declare-styleable>
在您的自定义视图构造函数中,尝试类似的操作(我没有 运行 它):
int defaultColor = 0xFFFFFF; // It may be anyone you want.
TypedArray attr = getTypedArray(context, attributeSet, R.styleable.YourTheme);
int textColor = attr.getColor(R.styleable.YourTheme_textColor, defaultColor);
应该是这样的:
变体 1
public MyCustomView(Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typed = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, defStyleRes);
int resId = typed.getResourceId(R.styleable.MyCustomView_custom_attr, R.drawable.default_resourceId_could_be_color);
Drawable drawable = getMultiColourAttr(getContext(), typed, R.styleable.MyCustomView_custom_attr, resId);
// ...
Button mView = new Button(getContext());
mView.setBackground(drawable);
}
protected static Drawable getMultiColourAttr(@NonNull Context context,
@NonNull TypedArray typed,
int index,
int resId) {
TypedValue colorValue = new TypedValue();
typed.getValue(index, colorValue);
if (colorValue.type == TypedValue.TYPE_REFERENCE) {
return ContextCompat.getDrawable(context, resId);
} else {
// It must be a single color
return new ColorDrawable(colorValue.data);
}
}
当然 getMultiColourAttr() 方法可以不是静态的也可以不受保护,这取决于项目。
思路是为这个特定的自定义属性获取一些resourceId,只有当资源不是color而是TypedValue.TYPE_REFERENCE时才使用它,这应该意味着有Drawable可以获取。一旦你得到一些 Drawable 应该很容易像背景一样使用它例如:
mView.setBackground(可绘制);
变体 2
查看变体 1,您可以使用相同的 resId,但只需将其传递给 View 方法 setBackgroundResource(resId),该方法将只显示该资源后面的任何内容 - 可能是可绘制或颜色。
希望对您有所帮助。谢谢
我在另一个答案中读到,在 android 中,您可以为具有多种格式的自定义视图声明属性,如下所示:
<attr name="textColor" format="reference|color"/>
如何在 class 中访问这些属性?我应该假设它是一个参考,使用 getResources().getColorStateList()
然后假设它是原始的 RGB/ARGB 颜色如果 Resources.getColorStateList()
抛出 Resources.NotFoundException
或者有更好的区分方法在 formats/types?
在你的/res/attrs.xml:
<declare-styleable name="YourTheme">
<attr name="textColor" format="reference|color"/>
</declare-styleable>
在您的自定义视图构造函数中,尝试类似的操作(我没有 运行 它):
int defaultColor = 0xFFFFFF; // It may be anyone you want.
TypedArray attr = getTypedArray(context, attributeSet, R.styleable.YourTheme);
int textColor = attr.getColor(R.styleable.YourTheme_textColor, defaultColor);
应该是这样的:
变体 1
public MyCustomView(Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typed = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, defStyleRes);
int resId = typed.getResourceId(R.styleable.MyCustomView_custom_attr, R.drawable.default_resourceId_could_be_color);
Drawable drawable = getMultiColourAttr(getContext(), typed, R.styleable.MyCustomView_custom_attr, resId);
// ...
Button mView = new Button(getContext());
mView.setBackground(drawable);
}
protected static Drawable getMultiColourAttr(@NonNull Context context,
@NonNull TypedArray typed,
int index,
int resId) {
TypedValue colorValue = new TypedValue();
typed.getValue(index, colorValue);
if (colorValue.type == TypedValue.TYPE_REFERENCE) {
return ContextCompat.getDrawable(context, resId);
} else {
// It must be a single color
return new ColorDrawable(colorValue.data);
}
}
当然 getMultiColourAttr() 方法可以不是静态的也可以不受保护,这取决于项目。
思路是为这个特定的自定义属性获取一些resourceId,只有当资源不是color而是TypedValue.TYPE_REFERENCE时才使用它,这应该意味着有Drawable可以获取。一旦你得到一些 Drawable 应该很容易像背景一样使用它例如:
mView.setBackground(可绘制);
变体 2
查看变体 1,您可以使用相同的 resId,但只需将其传递给 View 方法 setBackgroundResource(resId),该方法将只显示该资源后面的任何内容 - 可能是可绘制或颜色。
希望对您有所帮助。谢谢