Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ImageView字符串作为src_Java_Android_Eclipse_Android Activity - Fatal编程技术网

Java ImageView字符串作为src

Java ImageView字符串作为src,java,android,eclipse,android-activity,Java,Android,Eclipse,Android Activity,你好,我试图解释我的意思,但具体如下: <ImageView android:id="@+id/imageLogo" android:src="@drawable/logo" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> 我希望src是一个字符串,以便其他需要使用它的用户可以轻松地进行编辑。问题是android:sr

你好,我试图解释我的意思,但具体如下:

<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灵活,因此如果我的客户想要更改他/她的徽标,他/她只需要转到string.xml并更改字符串在徽标名称中的值。但是现在当我想到它时,他们已经需要访问drawable文件夹来更改他们的图像,所以感谢所有的帮助:D

android:src指的是drawable图像资源,您不能将其更改为字符串资源。如果需要,您可以将图像的名称更改为android:src=“@drawable/imagename”,并重命名相应目录中的文件。android:src指可绘制图像资源,您不能将其更改为字符串资源。如果需要,您可以将图像的名称更改为android:src=“@drawable/imagename”,并在相应的目录中重命名该文件

int resourceId = Activity.getResources().getIdentifier(getString(R.string.myComapnyLogo), "drawable", "your.package.name");
image.setImageResource(resourceId);
这里使用
getString(R.string.mycopnylogo)
在运行时从xml文件获取字符串。 参考这个

这里使用
getString(R.string.mycopnylogo)
在运行时从xml文件获取字符串。
请参阅此项。

在xml中不能将src作为字符串

但您可以在调用活动中编写自己的代码,使用字符串资源设置图像源。 为此,您必须将图像文件放在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);

在xml中不能将src作为字符串

但您可以在调用活动中编写自己的代码,使用字符串资源设置图像源。 为此,您必须将图像文件放在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);

我正在为我的一个客户制作这个应用程序,一个要求是你可以在strings.xml中编辑图像src,以便于编辑。此应用程序将是一种模板应用程序,因此我们可以将其集成到其他应用程序中。编辑:你所说的是对的,我可以这样做,但这不是最优的。我为我的一个客户制作这个应用程序,一个要求是你可以在strings.xml中编辑图像src,以便于编辑。此应用程序将是一种模板应用程序,因此我们可以将其集成到其他应用程序中。编辑:你所说的是对的,我可以这样做,但这不是最佳的。你可以用textview来做。看看你能不能解释一下
图像视图的
用例
,你可以用textview来做这件事。看一看,你能不能再解释一下
ImageView的
用例