[英]Unable to load url in webview in fragment
I am using below code in my fragment to load html file from assets folder in my project.我在我的片段中使用以下代码从我的项目中的资产文件夹加载 html 文件。
public class PrivacyPolicyFragment extends Fragment {
WebView myWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_privacy_policy, container, false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("file:///android_asset/myfile.html");
return view;
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
return super.shouldOverrideUrlLoading(view, url);
}
}
}
}
Below is the xml file for the fragment下面是片段的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.deadbrains.shareyourthought.PrivacyPolicy" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="@color/colorAccent" >
<WebView
android:id="@+id/mywebview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<RelativeLayout
android:id="@+id/add_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
</RelativeLayout>
</LinearLayout>
WebView into a Fragment (android.support.v4) WebView 成 Fragment (android.support.v4)
Android Fragment WebView Android 片段 WebView
But it doesn't load anything, nor does it gives any error.但它不会加载任何东西,也不会给出任何错误。 I went through many SO threads and tried different things but no luck.
我经历了许多 SO 线程并尝试了不同的事情,但没有运气。 here are the threads that I tried.
这是我尝试过的线程。
将此语句添加到您的根LinearLayout
android:orientation="vertical"
Add view.loadUrl(url);
添加
view.loadUrl(url);
inside shouldOverrideUrlLoading
在
shouldOverrideUrlLoading
里面
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.