【问题标题】:How to set Local Default to en from fragment如何从片段将本地默认设置为 en
【发布时间】:2016-08-19 11:24:29
【问题描述】:

我已经搜索了许多看起来像这样的问题,但没有在其中任何一个中找到我的答案。我想从片段中将本地默认设置为英语,我将此代码用于 onStart()onAttach()onCreateView() 但没有用

我的片段代码:

package com.demo.tomcatfire.taskmanagerui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.Locale;

public class MainFragment2 extends Fragment implements View.OnClickListener {
    private LinearLayout ll_display,ll_add,ll_about,ll_about2,ll_learnig,ll_learnig2;


    @Override
    public void onAttach(Context context) {

        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }

    @Override
    public void onStart() {

        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getContext().getResources().updateConfiguration(config,
                getContext().getResources().getDisplayMetrics());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main2, container, false);

        ll_about=(LinearLayout) rootView.findViewById(R.id.LL_aboute);
        ll_about2=(LinearLayout) rootView.findViewById(R.id.LL_aboute2);
        ll_display=(LinearLayout)rootView.findViewById(R.id.LL_display);
        ll_learnig=(LinearLayout) rootView.findViewById(R.id.LL_learning);
        ll_learnig2=(LinearLayout) rootView.findViewById(R.id.LL_learning2);
        ll_add=(LinearLayout)rootView.findViewById(R.id.LL_add);
        //------------------------------------------------------------------------------------------
        ll_add.setOnClickListener(this);
        ll_display.setOnClickListener(this);
        ll_learnig.setOnClickListener(this);
        ll_learnig2.setOnClickListener(this);
        ll_about.setOnClickListener(this);
        ll_about2.setOnClickListener(this);



        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.LL_aboute:
                break;
            case R.id.LL_aboute2:
                break;
            case R.id.LL_add:
                startActivity(new Intent(getContext(),AddTaskActivity.class));
                break;
            case R.id.LL_learning:
                break;
            case R.id.LL_learning2:
                break;
            case R.id.LL_display:
                startActivity(new Intent(getContext(),DisplayFinalActivity.class));

                break;

        }

    }

    @Override
    public void onResume() {
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getActivity().getBaseContext().getResources().updateConfiguration(config,
                getActivity().getBaseContext().getResources().getDisplayMetrics());
    }
}

【问题讨论】:

  • 您遇到了什么错误/崩溃?
  • @Talha : no error no crash , but didn't work and didn't work change local
  • 您是否检查了您的默认语言环境是否为英语 (en)?如果你想设置同样的东西怎么办?
  • @SandeepSharma : 不工作:(

标签: android localization fragment


【解决方案1】:

检查 onAttach(Context context) 是否被调用?

解决方案: 需要使用支持库片段,因为 this method 已添加到 API 23 中。 我用以下代码进行了测试。它正在工作。

 @Override
    public void onAttach(Context context) {
        super.onAttach(context); // this statement is missing
        Locale locale = new Locale("en");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }

另外你只需要在onAttach()方法中改变locale即可。

希望对你有所帮助。

【讨论】:

  • 它对我有用。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多