Frage

I have the next list item which shows two texts, one aligned to the left and one aligned to the right. For now, each text is placed in a different TextView:

enter image description here

I would like to use only one TextView. For that, I know I can use SpannableString, but I have hours trying to find out the way without success.

Can anyone tell me how I can place both texts in the same TextView aligning one to the left and one to the right?

Thanks in advance.

War es hilfreich?

Lösung

This is what i would do:

First you need measure lenght of right side text.

            float textsize = messageview.getPaint().getTextSize();
                int lenght = int lenght = textsize * text.length();

After that use leadingMarginSpan:

public class leadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2{
        private int margin;
        private int lines;

        public leadingMarginSpan2(int lines, int margin){
            this.lines = lines;
            this.margin = margin;
        }
        @Override
        public void drawLeadingMargin(Canvas arg0, Paint arg1, int arg2,
                int arg3, int arg4, int arg5, int arg6, CharSequence arg7,
                int arg8, int arg9, boolean arg10, Layout arg11) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getLeadingMargin(boolean first) {
            if(first){
                return margin;
            }
            else{
                return 0;
            }
        }

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

    }

Set span:

spantext.setSpan(new leadingMarginSpan2(1, getWidth() - lenght), 0, text.lenght, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top