android - Android中的不同语言没有资源文件
问题描述
我有一个关于 Android 应用程序的多种语言的问题。我知道为字符串使用资源文件是一种常见的做法(如果您忘记了,Android Studio 总是会提醒您这一点)。但是,我想知道如何为以下两个元素执行此操作:
- 具有不同语言内容的图片
- 类中未在 XML 布局文件中明确定义的字符串变量
你会如何处理这些问题?我会很感激每一条评论。
更新:这是我在代码中使用的字符串变量的示例(但不在 XML 布局文件中)。它是 AlertDialog 中的字符串“Comment”:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Comment");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
input.setSingleLine(false);
input.setLines(3);
input.setText(comment_Text);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
comment_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
解决方案
你也可以本地化你的drawables。
您可以通过为特定语言创建文件夹来执行此操作,方法与本地化字符串资源相同。
默认语言环境的可绘制示例:
res/drawable-hdpi/country_flag.png
不同语言环境的可绘制示例 (es_ES)
res/drawable-es-rES-hdpi/country_flag.png
更多信息请查看官方文档: https ://developer.android.com/training/basics/supporting-devices/languages
关于你的第二个问题;你的类也应该使用字符串资源,不要使用硬编码的字符串。
如何执行此操作的示例:
在这种情况下,字符串资源文件将包含:
<string name="title_comment">Comment</string>
<string name="button_ok">OK</string>
<string name="button_cancel">Cancel</string>
您的 AlertDialog 的代码将如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_comment);
final EditText input = new EditText(this);
input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
input.setSingleLine(false);
input.setLines(3);
input.setText(comment_Text);
builder.setView(input);
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
comment_Text = input.getText().toString();
}
});
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
推荐阅读
- c++ - 自由函数与成员函数的“经验法则”
- python - Jenkins API - 无法从特定的 fodler 获得工作
- java - 通过矩阵转换和通过四元数+加法转换有什么区别?
- hugo - 我的 blogdown 制作的 hugo 网站没有更新
- c# - Find common items in list of lists of user type
- c - 侦听多个端口并接受连接
- c - Simple circular linked list in c
- python - How to list keys and count with dictionary of dictionaries
- matlab - (Matlab) Transform a vector into matrix by keeping the numbers by pairs
- python - Django Join Query - Show Only Categories With One Post or More