gpt4 book ai didi

java - android java 横向模式下语言不会改变

转载 作者:行者123 更新时间:2023-12-01 09:08:31 26 4
gpt4 key购买 nike

我在这方面遇到了困难,不知道为什么会发生

我的 Android list

<activity
android:name=".Splash"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

现在当我加载我的 MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
orienta(this);

setLanguage(this, getLanguage(this, "en"));
setContentView(R.layout.activity_main_activity);

现在这些方向函数和语言代码如下

    public void orienta(Context c){
SharedPreferences spp = PreferenceManager.getDefaultSharedPreferences(c);
String ss = spp.getString("orientation", "portrait");

if (ss.equals("portrait")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
if (ss.equals("landscape")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}

private String getLanguage(Context c, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
return preferences.getString("language", defaultLanguage);
}

@SuppressWarnings("deprecation")
public void setLanguage(Context context, String languageCode) {
Locale locale = new Locale(languageCode);


// Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
context.getApplicationContext().getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("language", languageCode);
editor.apply();

}

现在的问题是在纵向模式下,每个 Activity 都会显示在设置中选择的语言

但在横向模式下,语言仅显示在 fragment 和导航菜单项中,在首选项 Activity 和其他 Activity 以及工具栏中,它是相同的英语如何修复它

我已经添加了

android:configChanges="locale|keyboardHidden|orientation|screenSize"

在 list 中定义的每个 Activity 中

编辑:-通过在 setcontentview 之前放入每个 Activity ,使横向模式正常工作不知道为什么会出现这个问题如果将来有人遇到这个问题并修复它,请发表评论仅在横向模式下出现

最佳答案

通过在每个 Activity 中调用 setlanguage 方法来修复横向模式下的问题,纵向模式只需在主 Activity 中调用即可正常工作,但要使语言在横向模式下工作,需要在每个 Activity 中调用

将来有人遇到此问题或已修复请发表评论

关于java - android java 横向模式下语言不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067388/

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