1
我已經搜索了許多看起來像這樣的問題,但沒有找到我的答案在他們中的任何一個。我想從片段設置本地默認爲英語,我用這個代碼onStart()
和onAttach()
和onCreateView()
,但沒有奏效如何設置本地默認值en從片段
我的片段代碼:
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
@Talha:沒有錯誤沒有崩潰,但沒有工作,沒有工作改變本地 – EhSAn
你檢查你的默認語言環境是英語(en)嗎?如果你想設置相同的東西呢? – Talha