我在我的片段中使用下面的代码从我的项目中的assets文件夹加载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)
但它不会加载任何内容,也不会产生任何错误。我经历了许多SO线程并尝试了不同的东西,但没有运气。这是我尝试的主题。
答案 0 :(得分:1)
将此语句添加到根LinearLayout
android:orientation="vertical"
答案 1 :(得分:0)
在view.loadUrl(url);
shouldOverrideUrlLoading
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
{
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}