简体   繁体   English

在Android Studio中通过数组显示图像

[英]Display images through array in Android Studio

I'm trying to re-rig a quiz app to instead of displaying text questions, will display images (Ishihara slides to be specific). 我正在尝试重新装备测验应用程序,而不是显示文本问题,而是显示图像(具体来说是石原幻灯片)。 For reference, here's a screenshot of the original quiz app: 供参考,以下是原始测验应用程序的屏幕截图: 原谅丑陋的黄色

When a user selects the true or false buttons, a toast appears with the result. 当用户选择“是”或“否”按钮时,将显示烤面包和结果。 Straightforward, and the previous/next buttons cycle through the questions. 直截了当,然后上一个/下一个按钮循环浏览问题。 The questions are being stored as such: 问题被这样存储:

final QuestionAndAnswer[] mQandA = new QuestionAndAnswer[]{
 /*1*/  new QuestionAndAnswer(R.string.question_northAmerica, true),
 /*2*/  new QuestionAndAnswer(R.string.question_antarctica, false),
 /*3*/  new QuestionAndAnswer(R.string.question_canada, false),
 /*4*/  new QuestionAndAnswer(R.string.question_madagascar, true),
 /*5*/ new QuestionAndAnswer(R.string.question_wonders, false)
};

I thought that pointing the new QuestionAndAnswer at R.drawable.imagename could accomplish this, but instead just displays the image's location as text. 我认为,将new QuestionAndAnswer指向R.drawable.imagename可以完成此操作,而只是将图像的位置显示为文本。 And changing it to new ImageView(R.drawable.imagename, true) throws the error: 并将其更改为new ImageView(R.drawable.imagename, true)会引发错误:

ImageView (android.content.Context, android.util.AttributeSet)in ImageView cannot be applied to (int,boolean) ImageView中的ImageView(android.content.Context,android.util.AttributeSet)无法应用于(int,boolean)

I'm sorry, I'm still very new to Android but as you've probably put together by now, I'm a student and don't have a choice but to do this. 抱歉,对于Android来说我还很陌生,但是正如您现在可能已经将它们放在一起一样,我是一名学生,别无选择,只能这样做。

If you have something like this on layout xml 如果布局xml中有类似的内容

<RelativeLayout ...
...
      <TextView android:id="@+id/text_view_id" ... />

...
</RelativeLayout>

You need to change it to 您需要将其更改为

<RelativeLayout ...
...
      <ImageView android:id="@+id/image_view_id" ... />

...
</RelativeLayout>

Then on your activity class, maybe you have something like below 然后在您的活动课上,也许您有类似下面的内容

TextView textView;

public void onCreate() {

    textView = (TextView) findViewById(R.id.text_view_id);

    ....

    textView.setText(mQandA[i].question)
}

Where mQandA[i].question is your question resource aka R.string.question_northAmerica Change it to 其中mQandA [i] .question是您的问题资源,又名R.string.question_northAmerica更改为

ImageView imageView;

public void onCreate() {

     imageView = (ImageView) findViewById(R.id.image_view_id);

     ...

     imageView.setImageResource(mQandA[i].question)

}

Where mQandA[i].question is your image resource aka R.drawable.imagename 其中mQandA [i] .question是您的图像资源,即R.drawable.imagename

EDIT 1 编辑1

change every appearance of 改变一切

textView.setText(mQandA[i].question)

with

imageView.setImageResource(mQandA[i].question)

counterpart 对方

You would have to display the questions in an ImageView. 您将不得不在ImageView中显示问题。

This means that you can't just point it to a drawable, you have to edit the XML layout files, and change much of the other parts of the program. 这意味着您不能仅仅将其指向可绘制对象,还必须编辑XML布局文件,并更改程序的许多其他部分。
This would include the actual code to display the questions. 这将包括显示问题的实际代码。

You could edit your question and include the actual Java and XML files. 您可以编辑问题并包括实际的Java和XML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 循环以随机显示数组中的图像(Android) - Loop to randomly display Images from Array (Android) 如何加载数组中的图像以更快地显示android - how to load images in array to faster display android 遍历数组元素OnClickListener android studio - Going through array element OnClickListener android studio 如何捕获多个图像并将图像发送到下一个活动并使用 CameraX [Android Studio] 显示它们 - How Can I Capture Multiple Images and send the images to next Activity and display them using CameraX [Android Studio] Java:读取目录数组以显示目录中包含的图像,以便能够在数组中前后移动 - Java: Reading an array of a directory to display the images contained in the directory to be able to go forwards and backwards through the array Android Studio设计展示 - Android Studio design display Android Studio - 如何使用按钮和 if-elseif 语句循环浏览我的图像? - Android Studio -How can I cycle through my images using a button and if-elseif statements? 如何通过PHP作为中间件在android中显示存储在C:/ xampp / htdocs / uploads文件夹中的图像 - How to display images which stored in C:/xampp/htdocs/uploads folder through PHP as middleware in android Android Studio-在线下载图像 - Android Studio - Download images online Android Studio可绘制图像迭代 - android studio drawable images iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM