【问题标题】:Webview to Work In a FragmentWebview 在片段中工作
【发布时间】:2017-07-21 23:31:44
【问题描述】:

我一直在浏览这个网站和谷歌搜索,但恐怕我被卡住了。我只是无法让 webview 在我的片段中工作。我在这里尝试了很多不同的代码,但一定有我遗漏的东西。

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="layout.GER1">

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
    </WebView>


</RelativeLayout>

Java

package layout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.a1cmanny1.bmi.R;

public class GER1 extends Fragment {

    public WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v=inflater.inflate(R.layout.fragment_ger1, container, false);
        mWebView = (WebView) v.findViewById(R.id.webview);
        mWebView.loadUrl("https://www.google.co.nz/");

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        return v;
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 代码中的一切看起来都不错。您面临的问题是什么?
  • 当我运行它时,webview 是空白的。它甚至没有尝试加载。可能我错过了在片段中启用 web 视图的活动?
  • 我也在使用选项卡式活动 - 滑动视图模板。我在清单文件中有

标签: javascript android-studio android-fragments webview


【解决方案1】:

我让它工作了。

我不知道为什么我的初始代码不起作用,但我发现了这篇文章: Webview not working in fragment 并复制了视频中的说明。之后,我添加了上面的代码,它运行良好。

我最初用于选项卡式活动的教程一定是不正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多