你好呀。
我想幫助我。我是 android Studio 編程的初學者。我想根據手機的默認語言在 Android Studio 中傳遞“特定圖片”。
示例:我有兩張圖片,圖 1:它有阿拉伯文字。圖2:有英文文字。
問。我想在陳述句中使用條件“if”和“else if”?
EX : if(語言是阿拉伯語){pass the picture 1 } else if(the language is English){pass the picture 2 }.....等多種語言。
- 拜托,我想要這個演算法的答案。
uj5u.com熱心網友回復:
您可以通過將每張圖片添加到res\drawable. 兩個版本的圖片必須具有相同的名稱,以便Android系統在檢查設備語言時選擇合適的圖片。
在您的示例中,您的語言環境是英語和阿拉伯語,因此您需要有兩個檔案夾:
\app\src\main\res\drawable-ar\myPicture.png<< 阿拉伯語版\app\src\main\res\drawable-en\myPicture.png<<< 英文版
兩張圖片具有相同的名稱,但根據您的需要,它們的內容不同。
uj5u.com熱心網友回復:
有兩種方法可以做到這一點:
第一種方式,使用switch和case(更有效):
yourImageViewID.setImageResource(R.drawable.image_for_other_languages);
switch(Locale.getDefault().getLanguage()) {
case "ar": {
//"ar" is the code of the Arabic language
yourImageViewID.setImageResource(R.drawable.arabic_image);
break;
}
case "en": {
//"en" is the code of the English language
yourImageViewID.setImageResource(R.drawable.english_image);
break;
}
}
第二種方式,使用if else(效率較低):
yourImageViewID.setImageResource(R.drawable.image_for_other_languages);
if (Locale.getDefault().getLanguage().equals("ar")) {
yourImageViewID.setImageResource(R.drawable.arabic_image);
}
else if (Locale.getDefault().getLanguage().equals("en")) {
yourImageViewID.setImageResource(R.drawable.english_image);
}
在這兩個方面,yourImageViewID是一個ImageView,別忘了3個影像添加到您的資產,一個是阿拉伯語,如果設備語言不是阿拉伯語將被用于另一個用于英語,而第三個用于其他語言(或英語),如果您想向應用程式添加更多語言,還可以添加更多影像
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372335.html
上一篇:將字串從緩沖區更改為int
