使用网络视图更改的默认语言

时间:2019-09-05 06:39:22

标签: android android-studio android-layout android-fragments android-webview

我的应用默认语言为西班牙语。我已经介绍了XML的Web视图。在此更改之前,语言为西班牙语。添加网络视图后,它会自动显示英语。如何解决此问题?

感谢您的提前帮助。 我已经使用过下面的代码。

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" />

3 个答案:

答案 0 :(得分:1)

您可以动态添加Web视图并在此之后再次初始化语言。

    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

它将起作用

答案 1 :(得分:0)

正在使用webView

 @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init here language again  then 
    setContentView(R.layout.activity_terms_of_use);

答案 2 :(得分:0)

经过大量测试并遵循其他答案的建议,我终于解决了这个问题。

WebView 的问题:

  • WebView 必须在活动中动态加载。第一次无法通过 Activity#setContentView() 从 XML 文件加载,整个活动的本地化将中断。但是,后续启动或 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>