Java 如何在Webview中的OnReceiveDerror上膨胀新布局?
每当Java 如何在Webview中的OnReceiveDerror上膨胀新布局?,java,android,xml,webview,layout-inflater,Java,Android,Xml,Webview,Layout Inflater,每当webview捕捉到错误并执行onreceivederror()方法时,我就尝试放大新的error.xml布局。我已经完成了一半,但问题是我得到了两种观点。。。我的error.xml视图以及*“网页未找到错误”*视图,请帮助我解决此问题 Fragment.java public class menu_1 extends Fragment { private WebView wv; private TextView tv; RelativeLayout layout; @Override p
webview
捕捉到错误并执行onreceivederror()
方法时,我就尝试放大新的error.xml布局。我已经完成了一半,但问题是我得到了两种观点。。。我的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>
<?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>
更新片段布局 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>
<?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>
您可以调用新活动,并在webview所在片段的GetErrorPost布局文件中设置error.xmlused@Hasmukhkachhatiya我正在片段中获取此webview,所以不想调用新活动来显示错误…@Pehlaj添加了片段文件。请参见下面的答案hanax alot@Pehlaj:)它起作用了。。。我可以用同样的方法处理imageview和其他小部件吗。在我需要的时候设置可见性。这也行吗?是的,您可以根据webview的成功/错误显示/隐藏任何视图