Error: Expected resource of type styleable [ResourceType] error

前端 未结 4 535
一向
一向 2020-12-29 20:57

Take a look at this code snippet. I am getting an error with the last line, because I am passing an \'index\' instead of a resource. I thought it was a lint issue and tried

相关标签:
4条回答
  • 2020-12-29 21:37

    I had the same issue when trying to build a signed apk. Solved it by adding @SuppressWarnings("ResourceType") to suppress the warning, now it works fine.

    0 讨论(0)
  • 2020-12-29 21:45

    So the way I fixed this in Kotlin is the following: (thanks to Simons post)

    companion object {
        private val attributes = intArrayOf(
                android.R.attr.paddingLeft,
                android.R.attr.paddingTop,
                android.R.attr.paddingBottom,
                android.R.attr.paddingRight)
    }
    
    init {
        val arr = context.obtainStyledAttributes(attrs, attributes)
    
        @StyleableRes
        var i = 0
    
        val leftPadding = arr.getDimensionPixelOffset(i++, 0)
        val topPadding = arr.getDimensionPixelOffset(i++, 0)
        val rightPadding = arr.getDimensionPixelOffset(i++, 0)
        val bottomPadding = arr.getDimensionPixelOffset(i, 0)
    
        arr.recycle()
    }
    
    0 讨论(0)
  • 2020-12-29 21:49
    @StyleableRes int index = 1;
    
    mPaddingRight = ta.getDimensionPixelSize(index, 0); 
    
    0 讨论(0)
  • 2020-12-29 21:54
    TypedValue outValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.paddingLeft, outValue, true);
    int paddingLeft = outValue.data;
    

    Repeat the same for android.R.attr.paddingRight

    0 讨论(0)
提交回复
热议问题