[英]Display images through array in Android Studio
我正在嘗試重新裝備測驗應用程序,而不是顯示文本問題,而是顯示圖像(具體來說是石原幻燈片)。 供參考,以下是原始測驗應用程序的屏幕截圖:
當用戶選擇“是”或“否”按鈕時,將顯示烤面包和結果。 直截了當,然后上一個/下一個按鈕循環瀏覽問題。 問題被這樣存儲:
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)
};
我認為,將new QuestionAndAnswer
指向R.drawable.imagename
可以完成此操作,而只是將圖像的位置顯示為文本。 並將其更改為new ImageView(R.drawable.imagename, true)
會引發錯誤:
ImageView中的ImageView(android.content.Context,android.util.AttributeSet)無法應用於(int,boolean)
抱歉,對於Android來說我還很陌生,但是正如您現在可能已經將它們放在一起一樣,我是一名學生,別無選擇,只能這樣做。
如果布局xml中有類似的內容
<RelativeLayout ...
...
<TextView android:id="@+id/text_view_id" ... />
...
</RelativeLayout>
您需要將其更改為
<RelativeLayout ...
...
<ImageView android:id="@+id/image_view_id" ... />
...
</RelativeLayout>
然后在您的活動課上,也許您有類似下面的內容
TextView textView;
public void onCreate() {
textView = (TextView) findViewById(R.id.text_view_id);
....
textView.setText(mQandA[i].question)
}
其中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)
}
其中mQandA [i] .question是您的圖像資源,即R.drawable.imagename
編輯1
改變一切
textView.setText(mQandA[i].question)
同
imageView.setImageResource(mQandA[i].question)
對方
您將不得不在ImageView中顯示問題。
這意味着您不能僅僅將其指向可繪制對象,還必須編輯XML布局文件,並更改程序的許多其他部分。
這將包括顯示問題的實際代碼。
您可以編輯問題並包括實際的Java和XML文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.