无法在片段中的 webview 中加载 url
Unable to load url in webview in fragment
我在我的片段中使用以下代码从我项目的资产文件夹中加载 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);
}
}
}
}
下面是片段的 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)
Android Fragment WebView
但是它没有加载任何东西,也没有给出任何错误。我经历了许多 SO 线程并尝试了不同的东西但没有运气。这是我试过的主题。
在shouldOverrideUrlLoading
里面添加view.loadUrl(url);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
将此语句添加到您的根 LinearLayout
android:orientation="vertical"
我在我的片段中使用以下代码从我项目的资产文件夹中加载 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);
}
}
}
}
下面是片段的 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)
Android Fragment WebView
但是它没有加载任何东西,也没有给出任何错误。我经历了许多 SO 线程并尝试了不同的东西但没有运气。这是我试过的主题。
在shouldOverrideUrlLoading
view.loadUrl(url);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
将此语句添加到您的根 LinearLayout
android:orientation="vertical"