2

複数の言語を扱う方法を学習するための小さなテスト アプリケーションを作成し、エミュレータを使用してテストしました。Android SDK で NetBeans を使用しています。アプリの多言語機能は問題なく機能しています。エミュレーターの使用が遅いため、自分のデバイス (HTC Inspire 4G) でアプリを実行しようとしました。以前に他のアプリケーションを使用したことがありますが、問題はありませんでした。しかし、今日、このシンプルなアプリで、1 番目の画面と 2 番目の画面を切り替えると、2 ページ目のボタンとテキストビューが縮小し続けます。これはエミュレータでは発生しません。ウィジェットは同じサイズのままです。私は過去に行ったことのないレイアウトに関しては何もしていないので、なぜボタンがうまくいかないのかわかりません. 関連するコードを以下に掲載します。私が見逃しているものを別の目が見つけてくれることを願っています。

何百万回もありがとう、ビル

更新: ボタンが縮小した後、携帯電話を縦向きから横向き (またはその逆) に回転すると、通常のデフォルト サイズに戻ることに気付きました。(ただし、言語はデフォルトの英語にリセットされます。)

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>
4

1 に答える 1

1

私はそれを考え出した!!それぞれの後:

Configuration config = new Configuration();

あなたは電話する必要があります:

config.setToDefaults();

http://developer.android.com/reference/android/content/res/Configuration.htmlから

Public Constructors Configuration() 無効な構成を構築します。このオブジェクトを有効にするには、setToDefaults() を呼び出す必要があります。

これを、ロケールの設定方法を学んだ質問/回答に投稿する必要があります。

于 2012-10-30T14:09:08.207 に答える