Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用多种格式访问自定义属性?_Android_Attributes_Custom View - Fatal编程技术网

Android 如何使用多种格式访问自定义属性?

Android 如何使用多种格式访问自定义属性?,android,attributes,custom-view,Android,Attributes,Custom View,我在另一个答案中读到,在android中,您可以为自定义视图声明具有以下多种格式的属性: <attr name="textColor" format="reference|color"/> 如何在类中访问这些属性?我是否应该假设它是一个引用,使用getResources().getColorStateList(),然后假设它是原始RGB/ARGB颜色,如果Resources.getColorStateList()抛出Resources.NotFoundException,或者在

我在另一个答案中读到,在android中,您可以为自定义视图声明具有以下多种格式的属性:

<attr name="textColor" format="reference|color"/>


如何在类中访问这些属性?我是否应该假设它是一个引用,使用
getResources().getColorStateList()
,然后假设它是原始RGB/ARGB颜色,如果
Resources.getColorStateList()
抛出
Resources.NotFoundException
,或者在/res/attrs.xml中有更好的方式区分格式/类型吗?

<declare-styleable name="YourTheme">
    <attr name="textColor" format="reference|color"/>
</declare-styleable>

应该是这样的:

<attr name="textColor" format="reference|color"/>
变体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);
    }
}
当然getMultiColor Rattr()方法可能不是静态的,也不受保护,这取决于项目

其思想是为这个特定的自定义属性获取一些resourceId,并且仅当资源不是color而是TypedValue.TYPE_引用时使用它,这意味着可以获取Drawable。一旦你得到了一些可绘制的图片,应该很容易使用,比如背景:

mView.后退地面(可拉深)

变体2

查看变量1,您可以使用相同的resId,但只需将其传递给查看方法setBackgroundResource(resId),该方法将只显示此资源后面的内容-可以是可绘制的或彩色的


我希望这会有帮助。谢谢

为什么这是公认的答案?我想我没有回答这个问题。如何检测输入是
reference
还是
color
?@Moon,
getColor()
方法自动解析颜色。方法文档建议:检索索引处属性的颜色值。如果该属性引用了包含复杂ColorStateList的颜色资源,则将返回集合中的默认颜色。