android-fragments 使用webview更改默认语言

nbysray5  于 2022-11-13  发布在  Android
关注(0)|答案(3)|浏览(227)

我的应用程序默认语言是西班牙语。我引入了XML格式的webview。在此更改之前,语言是西班牙语。添加webview后,它会自动显示英语。如何解决此问题?
谢谢你的帮助提前。我已经使用下面的代码了。
第一个

bvjveswy

bvjveswy1#

您可以动态添加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

它会起作用的

2izufjch

2izufjch2#

在具有webView活动中

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

to94eoyn3#

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

WebView的问题:

  • 必须在Activity中动态加载WebView。首次通过Activity#setContentView()从XML文件加载将不起作用,并且整个Activity的本地化将中断。但是,后续启动或Activity#recreate()将按预期工作。
  • 即使WebView是动态加载的,它在第一次加载时也不能正常工作。同样,后续的加载或Activity#recreate()将正常工作。

考虑到上述问题,解决方案涉及动态加载WebView,然后立即重新创建Activity。

具有Web视图的活动.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(this);
        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.
    }
}

带有web视图的活动.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>

相关问题