改变方向后虚线下划线位置改变 [英] Dotted underline position changed after changing orientation

查看:36
本文介绍了改变方向后虚线下划线位置改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了下面链接中的多行文本支持

风景:

解决方案

参考您在此问题中提到的原始问题,并假设您使用的是原始问题中的DottedUnderlineSpan",则需要进行以下更改:

注释跨度被替换为 ReplacementSpans,一旦您自己处理方向更改,必须明确重新计算.设置注释跨度后,请勿删除它们.它们将在方向更改后保持有效.

ReplacementSpans 需要改变.在方向改变时,删除替换跨度,允许布局继续,然后重新计算 ReplacementSpans,如下面的代码所示,应该这样做.

override fun onConfigurationChanged(newConfig: Configuration) {super.onConfigurationChanged(newConfig)val textView1 = findViewById(R.id.textView1)删除下划线跨度(textView1)textView1.viewTreeObserver.addOnPreDrawListener(对象:ViewTreeObserver.OnPreDrawListener {覆盖乐趣 onPreDraw(): Boolean {textView1.viewTreeObserver.removeOnPreDrawListener(this)替换注释(textView1)返回假}})}私人乐趣 removeUnderlineSpans(textView: TextView) {val text = textView.text 作为 Spannableval spans = text.getSpans(0, text.length, DottedUnderlineSpan::class.java)spans.forEach { span ->text.removeSpan(跨度)}textView.setText(文本,TextView.BufferType.SPANNABLE)}私人乐趣 replaceAnnotations(textView: TextView) {val text = SpannableString(textView.text)val 跨度 =text.getSpans(0, text.length, Annotation::class.java).filter { span ->span.key == ANNOTATION_FOR_UNDERLINE}如果 (spans.isNotEmpty()) {val 布局 = textView.layoutspans.forEach { span ->replaceAnnotationSpan(文本,跨度,布局)}textView.setText(文本,TextView.BufferType.SPANNABLE)}}

I have done the the multi line text support from below link Dotted underline TextView not wrapping to the next line using SpannableString in Android

After changing the orientation portrait to landscape dotted underline moved to other position

I have added 'android:configChanges="orientation"' in manifest for prevent on create call.

    <activity android:name=".Main2Activity"
        android:configChanges="orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


setAnnotation(
                spannableString,
                "production and conversion"
        )

Portrait:

Landscape:

解决方案

Referring to the original question you mention in this question and assuming that you are using the "DottedUnderlineSpan" from the original question, you will need to make the following changes:

The annotation spans are replaced with ReplacementSpans which must be explicitly recomputed ince you are handling orientation changes yourself. Once the annotation spans are set, do not remove them. They will remain valid across orientation changes.

The ReplacementSpans will, however, need to change. Upon orientation change, remove the replacement spans, allow layout to proceed and then recalculate the ReplacementSpans as shown in the following code and that should do it.

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)

    val textView1 = findViewById<TextView>(R.id.textView1)
    removeUnderlineSpans(textView1)
    textView1.viewTreeObserver.addOnPreDrawListener(
        object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                textView1.viewTreeObserver.removeOnPreDrawListener(this)
                replaceAnnotations(textView1)
                return false
            }
        }
    )
}

private fun removeUnderlineSpans(textView: TextView) {
    val text = textView.text as Spannable
    val spans = text.getSpans(0, text.length, DottedUnderlineSpan::class.java)
    spans.forEach { span ->
        text.removeSpan(span)
    }
    textView.setText(text, TextView.BufferType.SPANNABLE)
}

private fun replaceAnnotations(textView: TextView) {
    val text = SpannableString(textView.text)
    val spans =
        text.getSpans(0, text.length, Annotation::class.java).filter { span ->
            span.key == ANNOTATION_FOR_UNDERLINE
        }
    if (spans.isNotEmpty()) {
        val layout = textView.layout
        spans.forEach { span ->
            replaceAnnotationSpan(text, span, layout)
        }
        textView.setText(text, TextView.BufferType.SPANNABLE)
    }
}

这篇关于改变方向后虚线下划线位置改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆