当我意识到我需要使用片段而不是活动以便在所有屏幕上共享相同的导航菜单时,我的应用程序几乎完全完成,所以现在我正在制作我的所有活动java代码分段工作。我似乎无法弄清楚的一个问题是改变语言的微调器。我制作和翻译了英语(默认),西班牙语(ES)和法语(FR)字符串。当有人在微调器上选择西班牙语时,我希望它将应用程序的语言环境更改为西班牙语(es)&做一个吐司,说语言改为西班牙语!等。为了做到这一点,它必须重新启动片段,对吗?那么从设置片段开始设置片段,以便语言更新?现在,当我从微调器中选择一个选项时,我可以看到我在微调器上选择了它,但没有任何反应。没有吐司,没有语言,没有刷新片段等等。非常感谢所有的帮助!谢谢!!我将在下面发布代码!
package com.ezeapplications.quikflipfinal;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Locale;
import java.util.Set;
/**
* A simple {@link Fragment} subclass.
*/
public class SettingsFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
public SettingsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Button settupdatebtn = (Button) view.findViewById(R.id.setting_update_btn);
settupdatebtn.setOnClickListener(this);
Spinner langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.lang_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
langspinner.setAdapter(adapter);
return view;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
langspinner.setOnItemSelectedListener(this);
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You Have Selected English!", Toast.LENGTH_SHORT)
.show();
setLocale("en");
SettingsFragment fragmenten = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionen =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransactionen.replace(R.id.fragment_container, fragmenten);
fragmentTransactionen.commit();
langspinner.setSelection(1);
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"Has Seleccionado Español!", Toast.LENGTH_SHORT)
.show();
setLocale("es");
SettingsFragment fragmentes = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransactiones =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransactiones.replace(R.id.fragment_container, fragmentes);
fragmentTransactiones.commit();
langspinner.setSelection(2);
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"Vous Avez Sélectionné Le Français!", Toast.LENGTH_SHORT)
.show();
setLocale("fr");
SettingsFragment fragmentfr = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionfr =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransactionfr.replace(R.id.fragment_container, fragmentfr);
fragmentTransactionfr.commit();
langspinner.setSelection(3);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick (View v) {
SettingsFragment fragment = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
Toast.makeText(getActivity(), "Settings Updated!", Toast.LENGTH_SHORT).show();
};
Locale myLocale;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
refresh();
}
public void refresh() {
Fragment currentFragment = getFragmentManager().findFragmentByTag("fragment_tag_String");
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
}
}
答案 0 :(得分:0)
您可以分离并附上您的碎片。然后它将刷新视图,并且本地将被更改。
Fragment currentFragment = getFragmentManager().findFragmentByTag("FRAGMENT");
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
在您调用此片段的MainActivity中为SettingsFragment设置fragmentTag。 (例如:OnNavigationItemSelected()方法)。
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new YourSettingsFragment(),"Tag").commit();
答案 1 :(得分:0)
试试这个:
public class SettingsFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Spinner langspinner;
public SettingsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Button settupdatebtn = (Button) view.findViewById(R.id.setting_update_btn);
settupdatebtn.setOnClickListener(this);
langspinner = (Spinner) view.findViewById(R.id.settings_language_spinner);
langspinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.lang_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
langspinner.setAdapter(adapter);
langspinner.setSelection(0, false);
return view;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You Have Selected English!", Toast.LENGTH_SHORT)
.show();
setLocale("en");
langspinner.setSelection(1);
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"Has Seleccionado Español!", Toast.LENGTH_SHORT)
.show();
setLocale("es");
langspinner.setSelection(2);
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"Vous Avez Sélectionné Le Français!", Toast.LENGTH_SHORT)
.show();
setLocale("fr");
langspinner.setSelection(3);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
SettingsFragment fragment = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Toast.makeText(getActivity(), "Settings Updated!", Toast.LENGTH_SHORT).show();
}
Locale myLocale;
public void setLocale(String lang) {
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
refresh();
}
public void refresh() {
Fragment currentFragment = getFragmentManager().findFragmentByTag("fragment_tag_String");
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
}
}
确保从SettingsFragment
加载activity
时添加片段标记
喜欢这个;
SettingsFragment fragmentA = new SettingsFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.MainFrameLayout,fragmentA,"fragment_tag_String")
.addToBackStack(null).commit();