【问题标题】:Android: Display variable imageAndroid:显示可变图像
【发布时间】:2012-10-25 22:25:02
【问题描述】:

编辑。我尝试加载可变图像,但不知何故它不起作用。我想我有错误的 xml imageview 设置。

ImageView lblPic = new ImageView(this);
int resID = getResources().getIdentifier(pic, "drawable", getPackageName());
lblPic.setImageResource(resID);

xml 图像视图 像这样使用 xml 我希望 Java 中的代码将替换 android:src 定义的图像,但它没有。如果我不使用 android:src 它会导致错误。有什么建议吗?

【问题讨论】:

    标签: android image variables imageview


    【解决方案1】:

    如果你有图片的名字,你需要做的

    int resID = getResources().getIdentifier(filename, "drawable", getPackageName());
    

    其中filenameR.drawable.之后的部分,然后调用

    lblPic.setImageResource(resId);
    

    【讨论】:

    • 我必须在 xml 文件中制作 imageview 还是没有它也可以工作?
    • 您可以根据需要在代码中创建图像视图。我更喜欢用 xml 来做。它使您的代码更干净,并且在 xml 中设置某些属性比在代码中更容易。使用(ImageView) findViewById(R.id.imageViewIdFromXML)获取imageview,如果你在xml中定义它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多