你好,我试着解释一下我的意思,但它如下:
<ImageView
android:id="@+id/imageLogo"
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
我希望 src 是一个字符串,以便其他需要使用它的用户可以轻松编辑。问题是 android:src 不接受带有文件名的@string/logo。
编辑:再解释一下:我希望我的图像 src 是灵活的,所以如果我的客户想要更改他/她的 Logo ,他/她只需要转到 string.xml 并将值更改为字符串名称中有 Logo 的名称。但现在当我想到它时,他们已经需要访问 drawable 文件夹来更改他们的图像,所以感谢所有帮助:D
最佳答案
你不能在 xml 中将 src 作为字符串。
但是您在调用 Activity 中编写自己的代码以使用字符串资源设置图像源。 为此,你必须将你的图像文件放在 asset 中,你可以引用下面的代码
ImageView view = findViewById(R.id.imageLogo);
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
//R.string.yourstring will be like "sample.png"
istr = assetManager.open(getResources().getString(R.string.yourstring));
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
view.setImageBitmap(bitmap);
关于Java ImageView 字符串作为 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764282/