Android Fragment WebView

时间:2013-11-22 05:27:45

标签: android webview fragment

我为我的反对者创建应用

我有app的问题

我的应用已经滑动了 同样的应用程序Youtube。

http://i.stack.imgur.com/4hxlM.png

但是WebView的问题我有一个布局,它有webView

这是webView_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/background2"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:text="@string/aboutme"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

这是班级

package com.alyousafi.SeebClub;

import com.alyousafi.SeebClub.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TimeTible_forgment extends Fragment {

    public TimeTible_forgment(){}

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

        View rootView = inflater.inflate(R.layout.webView_layout, container, false);

        return rootView;
    }
}

任何人都可以帮助我如何在片段中使用WebView? 这是个大问题 请帮帮我&gt;&gt;&gt;

3 个答案:

答案 0 :(得分:11)

试试这个

       public class DisplayFragment extends Fragment {

        private String curURL;

        public void init(String url) {

            curURL = url;

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container,

                Bundle savedInstanceState) {

            View view = inflater
                    .inflate(R.layout.webview_layout, container, false);

            if (curURL != null) {

                WebView webview = (WebView) view.findViewById(R.id.web);

                webview.getSettings().setJavaScriptEnabled(true);

                webview.setWebViewClient(new webClient());

                webview.loadUrl(curURL);

            }

            return view;

        }

        public void updateUrl(String url) {
            curURL = url;
            WebView webview = (WebView) getView().findViewById(R.id.web);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl(url);
        }

        private class webClient extends WebViewClient {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return false;

            }

        }

    }

答案 1 :(得分:2)

我几天前得到的相同,我这样做了,可以在片段中显示网页

public class HeroesFragment extends Fragment {

    public HeroesFragment() {}


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

        View rootView = inflater.inflate(R.layout.heroes, container, false);
        WebView heroespage = (WebView) rootView.findViewById(R.id.heroeswiki);
        WebSettings webSettings = heroespage.getSettings();
        webSettings.setJavaScriptEnabled(true);
        heroespage.loadUrl("file:///android_asset/heroes/index.html");

        return rootView;
    }

}

是我的代码,由您更改Heroesfragment,并替换部分代码以匹配您的代码

答案 2 :(得分:0)

你应该试试这个。

public class WebView extends Fragment {
WebView wv;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
        View view = inflater.inflate(R.layout.web_fragment, container, false);

         wv=(WebView)view.findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl("https://www.google.com");

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
    }
        });
        return view;
    }

}