当我回去时,为什么我的应用程序按钮会缩小;在设备与模拟器之间的页面之间?

时间:2012-10-29 22:56:02

标签: android layout button

我写了一个小测试应用程序,用于学习如何使用多种语言,我使用模拟器来测试它。我将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>

1 个答案:

答案 0 :(得分:1)

我想通了!!每次之后:

Configuration config = new Configuration();

你必须致电:

config.setToDefaults();

来自http://developer.android.com/reference/android/content/res/Configuration.html

  

Public Constructors Configuration()构造一个无效的   组态。您必须为此对象调用setToDefaults()   有效的。

我必须将此帖子发布到问题/答案,我在那里学习了如何设置语言环境。