kotlin 格式化的html文本(Spannable)没有完全显示在textview和scrollview中

ojsjcaue  于 2022-12-13  发布在  Kotlin
关注(0)|答案(3)|浏览(127)

我尝试在我的应用中添加隐私政策页面,整个页面都是html内容,我使用this method使文本可滚动,但问题是完整的文本不显示在textview中,我不知道是什么导致了这个问题

如果你仔细看下面图片底部的文字,你会看到一些部分的字符没有出现,“我用白色大头针标记了它”

此隐私策略XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:padding="10dp"
    tools:context=".ui.PrivacyPolicyFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/ppTV"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.0"
             />
    </LinearLayout>

</ScrollView>

这个片段代码

class PrivacyPolicyFragment : Fragment(), MenuProvider {

    private var _binding: FragmentPrivacyPolicyBinding? = null
    private val binding get() = _binding!!

    private val pp =
        "<h1 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 36px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Privacy Policy for Dummy Blog</h1><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">At Dummy Blog, accessible from https://abtallaldigital.blogspot.com/, one of our main priorities is the privacy of our visitors. This Privacy Policy document contains types of information that is collected and recorded by Dummy Blog and how we use it.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">If you have additional questions or require more information about our Privacy Policy, do not hesitate to contact us.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">This Privacy Policy applies only to our online activities and is valid for visitors to our website with regards to the information that they shared and/or collect in Dummy Blog. This policy is not applicable to any information collected offline or via channels other than this website. Our Privacy Policy was created with the help of the&nbsp;<a href=\"https://www.termsfeed.com/privacy-policy-generator/\" style=\"background-color: transparent; box-sizing: border-box; text-decoration-line: none;\">TermsFeed Privacy Policy Generator</a>.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Consent</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">By using our website, you hereby consent to our Privacy Policy and agree to its terms.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Information we collect</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The personal information that you are asked to provide, and the reasons why you are asked to provide it, will be made clear to you at the point we ask you to provide your personal information.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">If you contact us directly, we may receive additional information about you such as your name, email address, phone number, the contents of the message and/or attachments you may send us, and any other information you may choose to provide.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">When you register for an Account, we may ask for your contact information, including items such as name, company name, address, email address, and telephone number.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">How we use your information</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">We use the information we collect in various ways, including to:</p><ul style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px; margin-top: 0px;\"><li style=\"box-sizing: border-box; text-align: left;\">Provide, operate, and maintain our website</li><li style=\"box-sizing: border-box; text-align: left;\">Improve, personalize, and expand our website</li><li style=\"box-sizing: border-box; text-align: left;\">Understand and analyze how you use our website</li><li style=\"box-sizing: border-box; text-align: left;\">Develop new products, services, features, and functionality</li><li style=\"box-sizing: border-box; text-align: left;\">Communicate with you, either directly or through one of our partners, including for customer service, to provide you with updates and other information relating to the website, and for marketing and promotional purposes</li><li style=\"box-sizing: border-box; text-align: left;\">Send you emails</li><li style=\"box-sizing: border-box; text-align: left;\">Find and prevent fraud</li></ul><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Log Files</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Dummy Blog follows a standard procedure of using log files. These files log visitors when they visit websites. All hosting companies do this and a part of hosting services' analytics. The information collected by log files include internet protocol (IP) addresses, browser type, Internet Service Provider (ISP), date and time stamp, referring/exit pages, and possibly the number of clicks. These are not linked to any information that is personally identifiable. The purpose of the information is for analyzing trends, administering the site, tracking users' movement on the website, and gathering demographic information.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Cookies and Web Beacons</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Like any other website, Dummy Blog uses 'cookies'. These cookies are used to store information including visitors' preferences, and the pages on the website that the visitor accessed or visited. The information is used to optimize the users' experience by customizing our web page content based on visitors' browser type and/or other information.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">For more general information on cookies, please read&nbsp;<a href=\"https://www.generateprivacypolicy.com/#cookies\" style=\"background-color: transparent; box-sizing: border-box; text-decoration-line: none;\">\"Cookies\" article from the Privacy Policy Generator</a>.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Advertising Partners Privacy Policies</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">.You may consult this list to find the Privacy Policy for each of the advertising partners of Dummy Blog</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Third-party ad servers or ad networks uses technologies like cookies, JavaScript, or Web Beacons that are used in their respective advertisements and links that appear on Dummy Blog, which are sent directly to users' browser. They automatically receive your IP address when this occurs. These technologies are used to measure the effectiveness of their advertising campaigns and/or to personalize the advertising content that you see on websites that you visit.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Note that Dummy Blog has no access to or control over these cookies that are used by third-party advertisers.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Third Party Privacy Policies</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Dummy Blog's Privacy Policy does not apply to other advertisers or websites. Thus, we are advising you to consult the respective Privacy Policies of these third-party ad servers for more detailed information. It may include their practices and instructions about how to opt-out of certain options.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">You can choose to disable cookies through your individual browser options. To know more detailed information about cookie management with specific web browsers, it can be found at the browsers' respective websites.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">CCPA Privacy Rights (Do Not Sell My Personal Information)</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Under the CCPA, among other rights, California consumers have the right to:</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Request that a business that collects a consumer's personal data disclose the categories and specific pieces of personal data that a business has collected about consumers.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Request that a business delete any personal data about the consumer that a business has collected.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Request that a business that sells a consumer's personal data, not sell the consumer's personal data.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">If you make a request, we have one month to respond to you. If you would like to exercise any of these rights, please contact us.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">GDPR Data Protection Rights</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">We would like to make sure you are fully aware of all of your data protection rights. Every user is entitled to the following:</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to access – You have the right to request copies of your personal data. We may charge you a small fee for this service.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to rectification – You have the right to request that we correct any information you believe is inaccurate. You also have the right to request that we complete the information you believe is incomplete.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to erasure – You have the right to request that we erase your personal data, under certain conditions.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to restrict processing – You have the right to request that we restrict the processing of your personal data, under certain conditions.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to object to processing – You have the right to object to our processing of your personal data, under certain conditions.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">The right to data portability – You have the right to request that we transfer the data that we have collected to another organization, or directly to you, under certain conditions.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">If you make a request, we have one month to respond to you. If you would like to exercise any of these rights, please contact us.</p><h2 style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.1; margin: 0px 0px 18px; text-align: left;\">Children's Information</h2><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Another part of our priority is adding protection for children while using the internet. We encourage parents and guardians to observe, participate in, and/or monitor and guide their online activity.</p><p style=\"background-color: white; box-sizing: border-box; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; margin: 0px 0px 20px; text-align: left;\">Dummy Blog does not knowingly collect any Personal Identifiable Information from children under the age of 13. If you think that your child provided this kind of information on our website, we strongly encourage you to contact us immediately and we will do our best efforts to promptly remove such information from our records. </p>"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        _binding = FragmentPrivacyPolicyBinding.inflate(inflater, container, false)

        val menuHost = requireActivity()
        menuHost.addMenuProvider(
            this,
            viewLifecycleOwner,
            androidx.lifecycle.Lifecycle.State.CREATED
        )

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

//        binding.ppTV.movementMethod = ScrollingMovementMethod()

        val html = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            HtmlCompat.fromHtml(pp, HtmlCompat.FROM_HTML_MODE_LEGACY)
        } else {
            @Suppress("DEPRECATION")
            Html.fromHtml(pp)
        } as Spannable

        binding.ppTV.text = html

    }

    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
    }

    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        if (menuItem.itemId == android.R.id.home) {
            findNavController().navigateUp()
        }
        return super.onContextItemSelected(menuItem)
    }

}
fiei3ece

fiei3ece1#

尝试此视图make滚动视图wrap_content

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textSize="16sp" />
            </ScrollView>

        </LinearLayout>
8yparm6h

8yparm6h2#

我想你是想让textview填充屏幕的剩余区域。
因此,将android:fillViewport="true"属性添加到ScrollView并使TextView成为match_parent

<ScrollView 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"
    android:padding="10dp"
    android:fillViewport="true"
    tools:context=".ui.PrivacyPolicyFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/ppTV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             />
    </LinearLayout>

</ScrollView>

**编辑:**如果除了唯一的TextView之外没有其他布局,则根本不需要滚动视图。

TextView的Below属性将使其可滚动。

android:scrollbars = "vertical"

所以最终布局应该是:

<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"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/ppTV"
        android:scrollbars = "vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
8i9zcol2

8i9zcol23#

我会将xml更改为如下形式:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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=".ui.PrivacyPolicyFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="top|center"
        android:padding="10dp">

        <TextView
            android:id="@+id/ppTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"/>

    </LinearLayout>

</ScrollView>

这样,如果你想扩大填充,用户仍然可以滚动时,触摸该区域。

相关问题