【问题标题】:how to access custom attribute with multiple formats?如何访问具有多种格式的自定义属性?
【发布时间】:2015-01-27 20:11:28
【问题描述】:

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

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

如何在我的班级中访问这些属性?如果Resources.getColorStateList()抛出Resources.NotFoundException,我应该假设它是一个参考,使用getResources().getColorStateList(),然后假设它是原始RGB/ARGB颜色,还是有更好的方法来区分格式/类型?

【问题讨论】:

    标签: android attributes custom-view


    【解决方案1】:

    应该是这样的:

    变体 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(drawable);

    变体 2

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

    我希望它会有所帮助。谢谢

    【讨论】:

      【解决方案2】:

      在您的 /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);
      

      【讨论】:

      • 为什么这个被接受的答案?没有回答我认为的问题。您是如何检测输入是reference 还是color
      • @Moon,getColor() 方法自动解析颜色。方法文档建议:检索索引处属性的颜色值。如果属性引用了包含复杂 ColorStateList 的颜色资源,则返回集合中的默认颜色。
      猜你喜欢
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      相关资源
      最近更新 更多