语言在横向模式android java中不会改变

时间:2016-12-09 19:15:31

标签: java android

我很难在此不知道为什么会发生这种情况

我的机器人清单

<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();

    }

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

但在横向模式下,语言仅显示在片段和导航菜单项中, 在preferenceactivty和其他活动中,也在工具栏中,它是相同的英语语言如何解决它

我添加了

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

在清单

中定义的每个活动中

编辑: - 通过在setcontentview之前插入每个活动使景观模式起作用 不知道为什么会出现这个问题 如果有人将来得到这个问题并修复它请评论 仅进入横向模式

1 个答案:

答案 0 :(得分:1)

通过在横向模式的每个活动修复问题中设置setlanguage方法调用,井纵向模式可以正常工作,只需在主要活动中调用它,但要使语言在横向模式下工作,它需要在每个活动中调用

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