gpt4 book ai didi

java - 强制 LeadingMarginSpan2 的文本方向

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:13:21 24 4
gpt4 key购买 nike

我正在按照建议使用 LeadingSpanMargin2 设置我的 TextView 样式,以便它围绕 Image 流动,问题是我的文本set 是动态的,可以是 rtl 或 ltr。

我尝试了所有我能想到的方法来修复 leadingMargin,以便它在两种情况下都在右或左,但无济于事。

此外,我已经尝试过在 github 中发布的 FlowTextView 小部件,但它在 rtl 情况下效果不佳,所以请不要建议。

非常感谢。

这是我使用的代码,这是在另一个答案中建议的:

public void setFlowText(CharSequence text, int width , int height, TextView messageView)
{
float textLineHeight = messageView.getPaint().getTextSize();

// Set the span according to the number of lines and width of the image
int lines = (int)Math.ceil(height / textLineHeight);
//For an html text you can use this line: SpannableStringBuilder ss = (SpannableStringBuilder)Html.fromHtml(text);
SpannableString ss = new SpannableString(text);
ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//ss.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(ss);

// Align the text with the image by removing the rule that the text is to the right of the image
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)messageView.getLayoutParams();
int[]rules = params.getRules();
rules[RelativeLayout.RIGHT_OF] = 0;
}

public class MyLeadingMarginSpan2 implements LeadingMarginSpan2 {
private int margin;
private int lines;
private boolean wasDrawCalled = false;
private int drawLineCount = 0;

public MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
}

@Override
public int getLeadingMargin(boolean first) {
boolean isFirstMargin = first;
// a different algorithm for api 21+
if (Build.VERSION.SDK_INT >= 21) {
this.drawLineCount = this.wasDrawCalled ? this.drawLineCount + 1 : 0;
this.wasDrawCalled = false;
isFirstMargin = this.drawLineCount <= this.lines;
}

return isFirstMargin ? this.margin : 0;
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
this.wasDrawCalled = true;
}

@Override
public int getLeadingMarginLineCount() {
return this.lines;
}
}

最佳答案

由于某些未知原因,您可以通过传递获得左边距:

ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

要获得正确的利润率,您需要通过:

ss.setSpan(new MyLeadingMarginSpan2(lines, width), 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

我不知道那里发生了什么,但这行得通!

关于java - 强制 LeadingMarginSpan2 的文本方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com