Android: is there a way to apply the right edge range in edittext?

I am looking for something similar to LeadingMarginSpan, but capable of applying both left and right margins to the text, not just the left edge.

What I'm trying to do is make some paragraphs narrower than others in the same editor, so it might look something like this:

blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah 

As far as I know, there are no predefined intervals for this, and I don’t know how to create a new range from scratch that could do this. Any help would be greatly appreciated!

+4
source share
3 answers

As far as I know, there are no predefined intervals that do this

AFAIK, you're right.

and I don’t know how to take a new step from scratch that could do it.

Well, LeadingMarginSpan support LeadingMarginSpan baked into Layout and StaticLayout , so just creating a TrailingMarginSpan will not be enough. You will need to create your own subclasses of Layout and StaticLayout , redefining and cloning their very complex draw() methods and inserting your TrailingMarginSupport .

In short, it will be painful.

If you happen to create your own firmware, it becomes much easier ... :-)

+2
source

I would switch to webview instead of TextView

With webview, you can use the html tag, which offers a lot more formatting options.

0
source

You cannot set the correct fill / margin at intervals. But you can cheat with MetricAffectingSpan . It has two methods: updateMeasureState (called when measuring text) and updateDrawState (called when drawing text).

So, if you increase the value of textScaleX when measuring, android will make short lines. If you do not increase textScaleX when drawing, android will not scale the lines when drawing. As a result, you will get shorter lines. It will look like the right addition.

This is not ideal: you cannot set the correct padding in pixels, but you will have some padding on the right.

Here is an example of a 5% correct fill.

 public class RoughtRightPaddingSpan extends MetricAffectingSpan { @Override public void updateMeasureState(TextPaint p) { p.setTextScaleX(1.05f); } @Override public void updateDrawState(TextPaint tp) { } } 
0
source

Source: https://habr.com/ru/post/1402962/


All Articles