使用 webview 更改默认语言
Using a webview changed default language
我的应用程序默认语言是西班牙语 (es)。我在XML中引入了一个webview。在此更改之前,语言是西班牙语。添加webview后,自动显示英文。我该如何解决这个问题?
感谢您提前提供帮助。
我也已经使用了下面的代码。
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString(String.valueOf(Locale.SPANISH));
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="-5dp"
tools:ignore="WebViewLayout" />
在activity有webView
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init here language again then
setContentView(R.layout.activity_terms_of_use);
您可以动态添加网页视图,然后再次初始化语言。
llDynemic=(LinearLayout)findViewById(R.id.test);
WebView webView = new WebView(getContext());// webview in mainactivity
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webView.setBackgroundColor(Color.TRANSPARENT);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;} a {color: #337ab7;}</style>" + newBody, "text/html", "UTF-8", null);
llDynemic.addView(webView);
// initialize the language here
它会起作用
经过大量测试并遵循其他答案的建议,我终于解决了这个问题。
WebView
的问题:
WebView
必须在 activity 中动态加载。首次通过 Activity#setContentView()
从 XML 文件加载将无法正常工作,整个 activity 的本地化将中断。但是,后续启动或 Activity#recreate()
将按预期工作。
- 即使
WebView
是动态加载的,第一次也不行。同样,后续启动或 Activity#recreate()
将正常工作。
考虑到上述问题,解决方案包括动态加载 WebView
,然后立即重新创建 activity。
ActivityWithWebView.java
public class ActivityWithWebView extends AppCompatActivity {
private WebView webView;
private static boolean firstTime = true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_with_web_view);
setSupportActionBar(findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.setTitle(R.string.instructions);
// WebView has to be loaded dynamically to prevent in-app localisation issue.
webView = new WebView(AppManager.getContext());
if (firstTime) {
// Recreate if loaded for the first time to prevent localisation issue.
recreate();
firstTime = false;
return;
}
LinearLayoutCompat webviewWrapper = findViewById(R.id.webview_wrapper);
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webviewWrapper.addView(webView);
// Do other works.
}
}
activity_with_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:colorBackground" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/webview_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我的应用程序默认语言是西班牙语 (es)。我在XML中引入了一个webview。在此更改之前,语言是西班牙语。添加webview后,自动显示英文。我该如何解决这个问题?
感谢您提前提供帮助。 我也已经使用了下面的代码。
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString(String.valueOf(Locale.SPANISH));
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="-5dp"
tools:ignore="WebViewLayout" />
在activity有webView
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init here language again then
setContentView(R.layout.activity_terms_of_use);
您可以动态添加网页视图,然后再次初始化语言。
llDynemic=(LinearLayout)findViewById(R.id.test);
WebView webView = new WebView(getContext());// webview in mainactivity
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webView.setBackgroundColor(Color.TRANSPARENT);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;} a {color: #337ab7;}</style>" + newBody, "text/html", "UTF-8", null);
llDynemic.addView(webView);
// initialize the language here
它会起作用
经过大量测试并遵循其他答案的建议,我终于解决了这个问题。
WebView
的问题:
WebView
必须在 activity 中动态加载。首次通过Activity#setContentView()
从 XML 文件加载将无法正常工作,整个 activity 的本地化将中断。但是,后续启动或Activity#recreate()
将按预期工作。- 即使
WebView
是动态加载的,第一次也不行。同样,后续启动或Activity#recreate()
将正常工作。
考虑到上述问题,解决方案包括动态加载 WebView
,然后立即重新创建 activity。
ActivityWithWebView.java
public class ActivityWithWebView extends AppCompatActivity {
private WebView webView;
private static boolean firstTime = true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_with_web_view);
setSupportActionBar(findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.setTitle(R.string.instructions);
// WebView has to be loaded dynamically to prevent in-app localisation issue.
webView = new WebView(AppManager.getContext());
if (firstTime) {
// Recreate if loaded for the first time to prevent localisation issue.
recreate();
firstTime = false;
return;
}
LinearLayoutCompat webviewWrapper = findViewById(R.id.webview_wrapper);
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
webviewWrapper.addView(webView);
// Do other works.
}
}
activity_with_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:colorBackground" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/webview_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>