WebView成片段(android.support.v4)

Kys*_*o84 4 android fullscreen fragment webview

我用了一个标签菜单ViewPager.每个选项卡都包含android.support.v4包中的片段(与旧SDK兼容).其中一个片段是WebView(调用FragmentWeb),我希望它保留在寻呼机布局中.问题是当我WebView充气时,它以全屏模式运行.

有没有办法让网页浏览器保持在我的标签下?

谢谢

我的片段类:FragmentWeb.java

public class FragmentWeb extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mainView = (View) inflater.inflate(R.layout.fragment_web, container, false);
    WebView webView = (WebView) mainView.findViewById(R.id.webview);
    webView.loadUrl("http://www.google.com");
    return mainView;
}
}
Run Code Online (Sandbox Code Playgroud)

My Fragment的布局:fragment_web.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

leo*_*o9r 8

您可以通过替换以下内容来简单地调整WebViewFragment的当前实现以满足您的需求:

import android.app.Fragment;
Run Code Online (Sandbox Code Playgroud)

通过

import android.support.v4.app.Fragment;
Run Code Online (Sandbox Code Playgroud)

在您自己的WebViewFragment.java源副本中.


Jus*_*ain 6

这可以通过将以下代码添加到片段代码中的onCreateView并嵌入WebViewClient调用来完成:

                webview.setWebViewClient(new MyWebViewClient());
            webview.getSettings().setPluginsEnabled(true);
            webview.getSettings().setBuiltInZoomControls(false); 
            webview.getSettings().setSupportZoom(false);
            webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
            webview.getSettings().setAllowFileAccess(true); 
            webview.getSettings().setDomStorageEnabled(true);
            webview.loadUrl(mTabURL);       
        }
        return v;
    }


    public class MyWebViewClient extends WebViewClient {        
        /* (non-Java doc)
         * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
         */


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.endsWith(".mp4")) 
            {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "video/*");

                view.getContext().startActivity(intent);
                return true;
            } 
            else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }
Run Code Online (Sandbox Code Playgroud)