如何将网页视图加载到一个片段?
FragmentActivity.java
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View rootView = inflater.inflate(R.layout.web, group, false);
wb = ((WebView)rootView.findViewById(R.id.webViewApp));
wb.getSettings().setJavaScriptEnabled(true);
pg = ((ProgressBar)rootView.findViewById(R.id.progressBar2));
wb.setWebViewClient(new WebViewClient());
wb.getSettings().setDomStorageEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setSupportMultipleWindows(true);
wb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wb.setHorizontalScrollBarEnabled(false);
wb.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wb.getSettings().setAllowFileAccessFromFileURLs(true);
wb.getSettings().setAllowUniversalAccessFromFileURLs(true);
wb.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt)
{
web.this.pg.setVisibility(0);
web.this.pg.setProgress(paramAnonymousInt);
if (paramAnonymousInt == 100) {
web.this.pg.setVisibility(8);
}
}
});
this.pg.setVisibility(0);
this.wb.loadUrl("http://stackoverflow.com/");
return rootView;
}
Web.xml中
<LinearLayout
android:orientation="vertical"
android:background="#ffffe0b2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
android:id="@id/progressBar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
<WebView
android:id="@id/webViewApp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="@id/progressBar2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
Screenshot 谢谢:))
答案 0 :(得分:0)
我认为,你必须简化你的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="@id/webViewApp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/progressBar2"/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal" />
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
并确保您的活动&#39; layout Fragment
的容器是below
工具栏,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/action_bar_height"/>
<FrameLayout
android:layout_below="@+id/toolbar"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>