gpt4 book ai didi

android - 当我在设备页面和模拟器页面之间来回切换时,为什么我的应用程序按钮会缩小?

转载 作者:行者123 更新时间:2023-11-29 02:00:02 25 4
gpt4 key购买 nike

我编写了一个小测试应用程序来学习如何使用多种语言,并使用模拟器对其进行了测试。我将 NetBeans 与 Android SDK 一起使用。该应用程序的多语言方面运行良好。由于使用模拟器的速度很慢,我尝试在我的设备(HTC Inspire 4G)上运行我的应用程序。我以前用过其他应用程序,没有问题。然而,今天,使用这个简单的应用程序,当我在第一屏和第二屏之间来回切换时,第 2 页上的按钮和 TextView 不断缩小。这不会发生在模拟器中;小部件保持相同的大小。我没有做任何我过去没有做过的布局方面的事情,所以我不知道为什么按钮会变坏。相关代码贴在下面。我希望另一双眼睛能找到我所缺少的东西。

一百万次感谢,比尔

更新:我注意到如果我旋转我的手机,在按钮缩小后从纵向到横向(反之亦然),它们会恢复到正常的默认大小。 (尽管语言重置为默认的英语。)

主要 Activity .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

Public Constructors Configuration() Construct an invalid Configuration. You must call setToDefaults() for this object to be valid.

我必须将其发布到我学习如何设置语言环境的问题/答案中。

关于android - 当我在设备页面和模拟器页面之间来回切换时,为什么我的应用程序按钮会缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13130849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com