如何在 WebView 中以分页的方式显示类似阅读器的 HTML 文件?

huangapple 未分类评论47阅读模式
英文:

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 中以分页的方式显示类似阅读器的 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-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.ViewPagerActivity&quot;&gt;


    &lt;androidx.viewpager.widget.ViewPager
        android:id=&quot;@+id/viewPager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;WebView
            android:id=&quot;@+id/webview_reader&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot; /&gt;

    &lt;/androidx.viewpager.widget.ViewPager&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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(&quot;file://&quot;+ViewPagerActivity.this.getFilesDir()+&quot;/a.html&quot;);

    }
}

Here's the output of the app-

如何在 WebView 中以分页的方式显示类似阅读器的 HTML 文件?]

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.

huangapple
  • 本文由 发表于 2020年4月9日 18:18:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61118914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定