英文:
How to show HTML file in webview with Page by page division like a Reader?
问题
在我的安卓项目中,我已经将一个HTML文件保存在应用程序中。现在,从一个Activity类中,我正在使用一个 webView 来显示这个 HTML文件 的内容。这个HTML文件包含了很长的文本,显示为 paragraph 标签内的一个段落。
因此,在 webView 中,我可以通过 垂直滚动 来阅读文本。但我需要一个建议,以便像阅读器调整移动屏幕一样,逐页显示整个内容。更清楚地说,我不希望用户垂直滚动,而是希望用户可以向右或向左滑动以切换从一页到另一页。
以下是布局的XML文件代码-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPagerActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview_reader"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
以下是我的Activity类的代码-
public class ViewPagerActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
webView = findViewById(R.id.webview_reader);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file://" + ViewPagerActivity.this.getFilesDir() + "/a.html");
}
}
以下是应用的输出-
我也尝试将 webview 显示在 Pageview 中,但也没有成功。因此,我希望这整个HTML页面能够通过调整移动屏幕来将字符分割,并像阅读器一样一页一页地显示。所以,如果有人能够用代码示例帮助我解决这个问题,我会非常感谢。
英文:
In my android project, I have saved one HTML file inside my application. Now, from an Activity class I am using a webView to show the contents of this HTML file. This html file contains very long amount of text showing as a paragraph within paragraph tag.
So, in webView I can read the text by scrolling vertically. But I need a suggestion to show the entire content, page by page like a reader adjusting the mobile screen. More clearly, i don't want user to scroll vertically, rather user will swap right or left to move from one page to another.
Here's the Layout xml file code-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPagerActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview_reader"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
And here's my Activity class code-
public class ViewPagerActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
webView = findViewById(R.id.webview_reader);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file://"+ViewPagerActivity.this.getFilesDir()+"/a.html");
}
}
Here's the output of the app-
I also tried to show the webview in Pageview but it also didn't work. So, I want this entire html page to divide the character by adjusting the mobile screen and show them page by page like a reader. So, it would be very nice if someone help me out this problem with coding example.
专注分享java语言的经验与见解,让所有开发者获益!
评论