Używam LeadingSpanMargin2
jak sugeruje mój TextView
projektować tak, że będzie przepływać wokół Image
, problem jest ustawić tekst jest dynamiczna i może być RTL lub ltr.Zmuszanie kierunek tekstu dla LeadingMarginSpan2
Próbowałem wszystko, co mogłem wymyślić, aby naprawić leadingMargin
, tak aby był albo prawo lub lewo w obu przypadkach, bez skutku.
Również wypróbowałem FlowTextView
widżet opublikowany w github i nie działa dobrze w przypadkach rtl, więc proszę nie sugeruj tego.
Wielkie dzięki.
Oto kod używam, który został zaproponowany w innym odpowiedź:
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;
}
}
Należy zwrócić uwagę na długość tekstu. tj .: ss.setSpan (..., 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Twoja długość tekstu powinna wynosić> 2! – gbero