【问题标题】:setImageResource() with attrs value具有 attrs 值的 setImageResource()
【发布时间】:2016-01-15 21:28:40
【问题描述】:

我正在尝试使用我定义的自定义属性将图像放入我的ImageView,我正在这样做:

属性:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="MyAttr">
        <attr name="my_image" format="reference"/>
    </declare-styleable>
</resources>

ImageView 属性:

app:my_image="@drawable/image"

比我的View:

int imageSrc;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyAttr, 0, 0);
try {
    imageSrc = ta.getResourceId(R.styleable.MyAttr_my_image, -1);
} finally {
    ta.recycle();
}

并将图像设置为ImageView

imageView.setImageResource(imageSrc);

但什么也没有出现,我也尝试过:

imageSrcDrawable = ta.getDrawable(R.styleable.MyAttr_my_image);

还有:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    imageView.setBackgroundDrawable(imageSrcDrawable);
} else {
    imageView.setBackground(imageSrcDrawable);
}

更新:

我已经尝试使用ta.getInt(..) 解析属性并且它工作正常!

我不明白为什么,提前谢谢!

【问题讨论】:

  • 你为什么不直接使用android:src="@drawable/image"
  • 因为我需要 LinearLayout 自定义类 @petey 的自定义属性
  • 这并不能真正解释你为什么不使用ImageView xml attr of android:src,而且看起来你可能正试图重新发明一个安卓已经给你的轮子。如果不是这种情况,请发布您的自定义 LinearLayout 类。
  • 可以,但假设我想做这样的事情?
  • 很公平,请发布您的“自定义”视图的完整代码

标签: java android imageview android-attributes


【解决方案1】:

如果您包含了 Layout XML 代码,它会很有用,但是,我将在黑暗中试一试,并建议您可能会遇到名称间距问题。您是否将布局 XML 中的命名空间定义为

xmlns:whatever="http://schemas.android.com/apk/res/com.yourcompany.yourpackage"

如果没有,我会试一试。其他建议:

我相信obtainStyledAttributes 在 AttributeSet 上调用它时实际上会解析所有属性。因此,如果 TypedArray 实例已经包含解析的资源 id,我不会感到惊讶,简而言之:尝试使用

imageSrc = ta.getInt(R.styleable.MyAttr_my_image, -1);

相对于ta.getResourceId()。最后,如果以上所有方法都失败了,我会尝试使用ta.hasValue(R.styleable.MyAttr_my_image) 来确定该值是否确实存在,如果不存在,那么至少你知道obtainStyledAttributes 没有成功解析和解析属性,因此,你可以开始调查原因。如文件位置、命名空间等。

希望你能成功。

编辑:

在阅读了您的 cmets 之后,我只得到了最后一个问题,从上面的 sn-ps 中,我看不出您实际上是在用哪种方法初始化它,我想知道您的图像视图是否在之后被重绘。我假设imageView.setImageResource(imageSrc); 使 imageView 无效,但是,因为它现在出现了,并且您刚刚确认您 100% 确定它已正确加载,那么我们可能值得手动使其无效,只是为了检查一下,那就是尝试调用image.setImageResource() 后的imageView.invalidate();

【讨论】:

  • 我有 java.lang.NumberFormatException: Invalid int: "res/drawable/image.png" on ta.getInt(...)
  • 好的,很酷。只是一个想法,至少现在你知道obtainStyledAttributes 正在做它的工作;-)
  • 我已经知道了,这就是为什么我不能完全理解它可能是什么!!!
  • 如果您包含该信息将会很有用 ;-) 您已确定它已被解析,通常是当有人尝试不同的方式检索数据和设置数据时。这表明他/她不确定发生了什么。无论如何,您确定在设置 src 后它会被重绘吗? (我会更新我的问题)
  • 尝试使用 invalidate() 和 postInvalidate() 但没有,如果我使用 setImageResource(R.drawable.image) 加载它可以工作..
【解决方案2】:

在这里试试这个代码

private int getResourceFromAttr(int attr)
{
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = getActivity().getTheme();
    theme.resolveAttribute(attr, typedValue, true);
    int res_ = typedValue.resourceId;
    return res_;
}

然后设置图片

    ImageView imageView = findViewById(R.id.main_logo);
    int res = getResourceFromAttr(R.attr.img_main_logo);
    imageView.setImageResource(res);

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多