java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10
发布日期:2021-09-30 11:34:34 浏览次数:7 分类:技术文章

本文共 1075 字,大约阅读时间需要 3 分钟。

获取自定义view属性的时候  遇到这个问题

int[] arr = new int[] { android.R.attr.layout_width, android.R.attr.layout_height };TypedArray typedArray = context.obtainStyledAttributes(attrs, arr);typedArray.getDimensionPixelSize(1, ViewGroup.LayoutParams.MATCH_PARENT);

如果 android:layout_height="match_parent" 

typedArray.getDimensionPixelSize(1, ViewGroup.LayoutParams.MATCH_PARENT);
这行代码执行的时候会报异常

java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10

搞了好久才搞明白0x10的含义

/** * Container for a dynamically typed data value.  Primarily used with * {@link android.content.res.Resources} for holding resource values. */public class TypedValue {   /** Identifies the start of plain integer values.  Any type value     *  from this to {@link #TYPE_LAST_INT} means the     *  data field holds a generic integer value. */    public static final int TYPE_FIRST_INT = 0x10;

layout_height="100dp" 只有该属性的值为具体数值的时候  才不会报异常,这个时候  TypedValued的值类型为TYPE_DIMENSION

/** The data field holds a complex number encoding a     *  dimension value. */    public static final int TYPE_DIMENSION = 0x05;

转载地址:https://blog.csdn.net/id19870510/article/details/52982311 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:读取layout属性-AttributeSet TypedArray
下一篇:android dialog dismiss cancel hide 的区别

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月23日 21时20分08秒