Android:如何从字符串设置ImageView src?
我很初学java,请帮忙。我有一个Android应用程序源代码。我默认ImageView的是这样的:Android:如何从字符串设置ImageView src?
<ImageView
android:id="@+id/flag_logo"
android:layout_marginBottom="@dimen/global_keyline_s"
android:scaleType="fitCenter"
android:src="@drawable/flag_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:adjustViewBounds="true" />
我需要我的应用程序在某些国家的翻译,我需要动态设置应用程序标题和标志图标。
我有字符串资源,并已翻译:
<resources>
<string name="app_name">My App Name</string>
<string name="flag_icon">@drawable/flag_icon</string>
...
</resources>
在ImageVeiw,我可以改变:
android:src="@drawable/flag_icon"
到android:src="@string/flag_icon"
?
当我测试这个脚本模拟器它是好的,但是当我尝试生成到签署的apk我得到错误:
Error:(4) Error: Unexpected resource reference type; expected value of type @string/ [ReferenceType]
我一直在寻找这个问题,但还没有找到,请帮忙。谢谢。
我认为你的方法是一个坏主意。
您也可以本地化drawable。我认为在本地化你的应用时,这是改变图像的更好方法。
在ImageVeiw,我可以改变:
android:src="@drawable/flag_icon" to android:src="@string/flag_icon" ?
不,你不能。什么@drawable/flag_icon
意味着可绘制的flag_icon
位于drawable
文件夹中。除非你有一个名称为 string
的文件夹,该路径无效
就本地化而言,只有一些特定的res文件夹可以本地化。有关它的更多信息here。
对于drawable,你应该使用类似drawable-[language code]-r[capitalized localization code]
的东西。
而且,由于数据值不匹配,您的方法将无法工作(请参阅不工作)。 @drawable
返回int
。
就像@glethien建议。我将使用本地化的绘图 - https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
因此,您将图像置于drawable-hdpi和drawable-de-hdpi中,并根据设备语言采用相应的资源。
我有个问题。为什么? – Blackbelt
您无法将imageview src更改为字符串src。 Imageview src不过是图像文件。 – Shriram
android:src =“@ drawable/flag_icon”是一个整数值,并且您正在调用一个字符串值,问题是为什么? –