[英]How to inflate new layout on OnRecievedError in Webview?
我想,只要誇大新error.xml布局webview
捕捉錯誤並執行onRecievedError()
方法。 我已完成一半,但問題是我同時獲得了兩個視圖...我的error.xml視圖以及*“找不到網頁錯誤” *視圖,所以請為此提供幫助。
Fragment.java
public class menu_1 extends Fragment {
private WebView wv;
private TextView tv;
RelativeLayout layout;
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_1, container, false);
wv = (WebView) v.findViewById(R.id.frag1_web);
wv.loadUrl("file:///res/raw/web1.html");
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
LayoutInflater inflater1=getActivity().getLayoutInflater();
View v=inflater1.inflate(R.layout.show_error,null);
view.addView(v);
}
});
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Menu 1");
}
}
show_error.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/errorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Problem Loading Page"
android:textColor="@android:color/black"
android:textSize="24sp" />
</RelativeLayout>
Fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<WebView
android:id="@+id/frag1_web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
更新片段布局
Fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<WebView
android:id="@+id/frag1_web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/errorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:text="Problem Loading Page"
android:textColor="@android:color/black"
android:textSize="24sp" />
</RelativeLayout>
隱藏錯誤的網頁視圖並顯示文本視圖
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
webView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.