Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改方向后,虚线下划线位置已更改_Android_Textview_Spannablestring - Fatal编程技术网

Android 更改方向后,虚线下划线位置已更改

Android 更改方向后,虚线下划线位置已更改,android,textview,spannablestring,Android,Textview,Spannablestring,我已经做了多行文字支持从下面的链接 将方向纵向更改为横向后,虚线下划线移动到其他位置 我在清单中添加了“android:configChanges=“orientation”,用于阻止创建调用 <activity android:name=".Main2Activity" android:configChanges="orientation"> <intent-filter> <action andr

我已经做了多行文字支持从下面的链接

将方向纵向更改为横向后,虚线下划线移动到其他位置

我在清单中添加了“android:configChanges=“orientation”,用于阻止创建调用

    <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"
        )

景观:

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

注释跨距将替换为replacementspands,由于您自己正在处理方向更改,因此必须显式重新计算该跨距。设置注释跨距后,不要删除它们。它们将在方向更改期间保持有效

然而,替换跨度将需要更改。方向更改后,删除替换跨距,允许布局继续,然后重新计算替换跨距,如以下代码所示,这应该可以做到

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)
    }
}
覆盖配置更改(newConfig:Configuration){
super.onConfigurationChanged(newConfig)
val textView1=findViewById(R.id.textView1)
removeUnderlineSpans(文本视图1)
textView1.viewTreeObserver.addOnPreDrawListener(
对象:ViewTreeObserver.OnPreDrawListener{
重写onPreDraw():布尔值{
textView1.viewTreeObserver.removeOnPreDrawListener(此)
替换注释(文本视图1)
返回错误
}
}
)
}
私人娱乐removeUnderlineSpans(文本视图:文本视图){
val text=textView.text作为span启用
val span=text.getspan(0,text.length,DottedUnderlineSpan::class.java)
span.forEach{span->
text.removeSpan(span)
}
textView.setText(text,textView.BufferType.SPANNABLE)
}
专用注释(textView:textView){
val text=SpannableString(textView.text)
瓦尔跨度=
getspan(0,text.length,Annotation::class.java).filter{span->
span.key==下划线的注释\u
}
if(span.isNotEmpty()){
val layout=textView.layout
span.forEach{span->
替换注释span(文本、跨度、布局)
}
textView.setText(text,textView.BufferType.SPANNABLE)
}
}

更改方向时,您必须重新计算位置。
android:configChanges=“orientation | screenSize | keyboardHidden”