我在WebView中显示带有文本的本地html文件。
如何根据手机的语言加载英文版?
这是我的代码:
public class Seccion1 extends Fragment {
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.seccion1, container, false);
mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);
//Archivo de ejemplo. Cambiar por el adecuado.
mWebView.loadUrl("file:///android_asset/productividad/GooglePlay2.html");
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setLongClickable(true);
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
// Buscar AdView como recurso y cargar una solicitud.
AdView adView = (AdView)this.getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Uri uri = Uri.parse("https://www.twitter.com/XXX");
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
return true;
}
return super.onOptionsItemSelected(item);
}
}
谢谢!
P.P:我正在添加这个文本,因为它说我的帖子主要是代码。我认为问题很清楚。编辑1:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.seccion4, container, false);
mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);
//Archivo de ejemplo. Cambiar por el adecuado.
//mWebView.loadUrl("file:///android_asset/textos_ES/Extra.html");
String language= Locale.getDefault().getLanguage();
String imageUrl="";
if (language.equals("Spanish")){
imageUrl="file:///android_asset/textos/Extra.html";
}
else {
imageUrl="file:///android_asset/textos/Ejemplo1.html";
}
mWebView.loadUrl(imageUrl);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setLongClickable(true);
mWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
return rootView;
}
答案 0 :(得分:1)
我认为@Duggu已经说清楚了。你可以试试像
这样的东西String language=Locale.getDefault().getLanguage();
String imageUrl="";
if (language.equals("English")){
imageUrl="english.html"
}
if (language.equals("French")){
imageUrl="french.html"
}
webView.loadUrl(imageUrl);
答案 1 :(得分:0)
尝试以下代码: -
Locale.getDefault().getDisplayLanguage();
以上方法返回当前设备的语言。