我花了数小时寻找答案,并且真的不知道如何解决它。因此,让我们言归正传:在图像周围流动textview
有一个图像和TextView
,我需要流动TextView
的ImageView
周围像这样:
第一个可能的解决方案woult是使用https://github.com/deano2390/FlowTextView但它不扩展TextView
所以这个库不适合我的原因数量。
第二种解决方案是使用LeadingMarginSpan.LeadingMarginSpan2
跨度,但它影响每个段落文本内的每个n行(像这样的答案 - >How to layout text to flow around an image),所以我得到水木清华这样的:
但我想只为前n行设置保证金!然后我决定执行LeadingMarginSpan.Standart
并创建一个计数器并在getLeadingMargin(first: Boolean): Int
函数调用中增加它。当计数器达到期望值时,函数返回0作为边距宽度。再次失败!而不是填充TextView
行,文本只是左移,并没有传播到视图的尽头!
UPD:是的,我在这里使用onGlobalLayoutListener
那么,谷歌搜索另一种解决方案,我发现这个答案https://stackoverflow.com/a/27064368/7218592 好吧,我所描述和实现的代码所做的一切:
//set left margin of desirable width
val params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
params.leftMargin = holder.imageContainerHeight!!
params.addRule(RelativeLayout.BELOW, holder.mNumberAndTimeInfo!!.id)
holder.mCommentTextView!!.layoutParams = params
if (holder.commentTextViewOnGlobalLayoutListener != null)
holder.mCommentTextView!!.viewTreeObserver.removeOnGlobalLayoutListener(
holder.commentTextViewOnGlobalLayoutListener)
//add onGlobalLayoutListener
holder.mCommentTextView!!.viewTreeObserver.addOnGlobalLayoutListener(
if (holder.commentTextViewOnGlobalLayoutListener != null)
holder.commentTextViewOnGlobalLayoutListener
else CommentTextViewOnGlobalLayoutListener(holder,
SpannableString(HtmlCompat.fromHtml(
mView.getActivity(), commentDocument.html(), 0,
null, SpanTagHandlerCompat(mView.getActivity())))))`
我OnGlobalLayoutListener
看起来是这样的:`
class CommentTextViewOnGlobalLayoutListener(
val holder: CommentAndFilesListViewViewHolder, val commentSpannable: Spannable) :
ViewTreeObserver.OnGlobalLayoutListener {
val LOG_TAG: String = CommentTextViewOnGlobalLayoutListener::class.java.simpleName
override fun onGlobalLayout() {
holder.mCommentTextView!!.viewTreeObserver.removeGlobalOnLayoutListener(this)
//when textview layout is drawn, get the line end to spanify only the needed text
val charCount = holder.mCommentTextView!!.layout.getLineEnd(Math.min(
holder.mCommentTextView!!.layout.lineCount - 1,
CommentLeadingMarginSpan.computeLinesToBeSpanned(holder)))
if (charCount <= commentSpannable.length) {
commentSpannable.setSpan(CommentLeadingMarginSpan(holder),
0, charCount, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
//set the left margin back to zero
(holder.mCommentTextView!!.layoutParams as RelativeLayout.LayoutParams).leftMargin = 0
holder.mCommentTextView!!.text = commentSpannable
}
}
`
那么,它的工作原理。但它有多可怕!当我使用视图持有者模式时,我必须将一个变量保存到监听器中,并且如果它未被调用并且被成功移除,则将其删除,因为onGlobalLayout
函数未被及时调用!它被称为太晚了,所以你需要等待约300毫秒,然后观看TextView
的所有“重建”,它看起来令人作呕!
所以,我的问题是: 如何使利润率第一 n行的TextView
,它被画在UI之前?
我会尝试不同的方法在这里,我将在这里使用网页视图,设置图像和文字在那里 –
Webview是不是因为有一个选项,几个自定义可点击的跨越,我需要处理。我不能放弃他们:( – koresuniku
可能重复[如何布局文本流动图像周围](https://stackoverflow.com/questions/2248759/how-to-layout-text-to-flow-around-一个图像) –