如何将片段中的本地默认值设置为 en

EhS*_*SAn 2 android localization fragment

我搜索了无数与此类似的问题,但没有在其中任何一个中找到我的答案。我想将片段中的本地默认设置设置为英语,我使用此代码来和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());
    }
}
Run Code Online (Sandbox Code Playgroud)

San*_*rma 7

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

解决方案: 需要使用您正在使用的支持库片段,因为该方法已在 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());
    }
Run Code Online (Sandbox Code Playgroud)

另外,您只需要在 onAttach() 方法中更改区域设置。

希望这对您有帮助。