[英]Why are my App's buttons shrinking when I go back & forth between pages on a device versus emulator?
我编写了一个小测试应用程序,以学习如何使用多种语言,并使用仿真器对其进行了测试。 我将NetBeans与Android SDK结合使用。 该应用程序的多语言方面运行正常。 由于使用模拟器的速度较慢,因此我尝试在设备(HTC Inspire 4G)上运行我的应用程序。 我以前在其他应用程序上做过,没有问题。 但是,今天,通过这个简单的应用程序,当我在第一屏幕和第二屏幕之间来回切换时,第2页上的按钮和textview不断缩小。 在仿真器中不会发生这种情况。 小部件保持相同的大小。 我没有做过过去没有做过的布局方面的事情,所以我不知道为什么按钮会变乱。 相关代码如下。 我希望另一双眼睛可以找到我所缺少的。
感谢倍百万,比尔
更新:我注意到,如果在缩小按钮后将手机从纵向旋转到横向(反之亦然),它们会恢复为默认的默认大小。 (尽管语言重置为默认的英语。)
MainActivity.java:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button;
button = (Button) findViewById(R.id.use_english_button);
button.setOnClickListener(new GoToPage2OnClickListener("en"));
button = (Button) findViewById(R.id.use_french_button);
button.setOnClickListener(new GoToPage2OnClickListener("fr"));
button = (Button) findViewById(R.id.use_spanish_button);
button.setOnClickListener(new GoToPage2OnClickListener("es"));
button = (Button) findViewById(R.id.use_italian_button);
button.setOnClickListener(new GoToPage2OnClickListener("it"));
}
private class GoToPage2OnClickListener implements View.OnClickListener {
private String language = "";
public GoToPage2OnClickListener(String language) {
this.language = language;
}
public void onClick(View v) {
// This comes from
// http://stackoverflow.com/questions/12230553/android-how-to-change-the-application-language-at-runtime
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferences languagepref =
getSharedPreferences("language", MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad", language);
editor.commit();
Intent intent = new Intent(MainActivity.this,
Page2Activity.class);
startActivity(intent);
}
}
Page2Activity.java:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.page_2);
SharedPreferences language =
getSharedPreferences("language", MODE_PRIVATE);
Map<String,String> prefMap = (Map<String,String>) language.getAll();
String str = prefMap.get("languageToLoad");
Locale locale = new Locale(str);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
DisplayMetrics dm = getBaseContext().getResources().getDisplayMetrics();
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
Button button;
button = (Button) findViewById(R.id.page_2_back_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
page_2.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/output_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:text="@string/welcome_b"
/>
<TableLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/page_2_back_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/page_2_back_button"
android:layout_margin="5sp"
android:layout_weight="1"
/>
<Button
android:id="@+id/page_2_next_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/page_2_next_button"
android:layout_margin="5sp"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
我想到了!! 每次之后:
Configuration config = new Configuration();
您必须致电:
config.setToDefaults();
来自http://developer.android.com/reference/android/content/res/Configuration.html
公共构造函数Configuration()构造一个无效的Configuration。 您必须调用setToDefaults()才能使该对象有效。
我将不得不将其发布到我了解如何设置语言环境的问题/答案中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.