Forcer le mot suivant sur une nouvelle ligne si le mot est trop long pour la visualisation de texte

J’ai un TextView , qui lorsqu’il est rempli par programme ne sera pas correctement le saut de ligne sur les mots.

Sortie actuelle:

C’est ce qui se passe:

 | Hi I am an examp | | le of a ssortingng t | | hat is not break | | ing correctly on | | words | 

Production attendue:

Je veux ceci:

 | Hi I am an | | example of a | | ssortingng that is | | breaking | | correctly on | | words | 

Java:

 Ssortingng mQuestion = "Hi I am an example of a ssortingng that is breaking correctly on words"; mTextView.setText(mQuestion); 

XML:

              

Vous pouvez d’abord obtenir le texte en utilisant TextView.getPaint() , puis chaque fois que vous ajoutez un nouveau mot (Hi, I, am, etc.), appelez measureText sur la peinture. Si la longueur du résultat est supérieure à la largeur disponible de votre TextView, ajoutez un \n avant le nouveau mot. Réinitialisez les données et répétez les étapes.

J’ai fini par utiliser

 private void initView() { Paint paint = new Paint(); float width = paint.measureText(mQuestion); int maxLength = 300; // put whatever length you need here if (width > maxLength) { List arrayList = null; Ssortingng[] array = (mQuestion.split("\\s")); arrayList = Arrays.asList(array); int seventyPercent = (int) (Math.round(arrayList.size() * 0.70)); // play with this if needed Ssortingng linebreak = arrayList.get(seventyPercent) + "\n"; arrayList.set(seventyPercent, linebreak); mQuestion = TextUtils.join(" ", arrayList); mQuestion.replace(",", " "); } mQuestionHolderTextView.setText(mQuestion); } 

Je mesure la chaîne, la transforme en une liste, puis je la divise à 70% et crée une nouvelle ligne. Ensuite, je transforme la liste en chaîne et supprime les virgules. Tant que le mot ne représente pas plus de 30% de la ligne restante, vous êtes en clair, sinon ajustez en conséquence.

C’est rapide et sale, mais cela a fonctionné pour moi.

En utilisant la méthode suivante, vous pouvez obtenir le texte enveloppé.

Comme je n’ai pas configuré Android, j’ai donc écrit une classe de test et appelé la méthode principale. Vous devez passer la largeur textview. Je suis passé 14 ici.

  public class Test{ public static void main(Ssortingng[] args) { Ssortingng wrappedText=wrapText(14); System.out.println(wrappedText); } public static Ssortingng wrapText(int textviewWidth) { Ssortingng mQuestion = "Hi I am an example of a ssortingng that is breaking correctly on words"; Ssortingng temp = ""; Ssortingng sentence = ""; Ssortingng[] array = mQuestion.split(" "); // split by space for (Ssortingng word : array) { if ((temp.length() + word.length()) < textviewWidth) { // create a temp variable and check if length with new word exceeds textview width. temp += " "+word; } else { sentence += temp+"\n"; // add new line character temp = word; } } return (sentence.replaceFirst(" ", "")+temp); } } 

Sortie -

 Hi I am an example of a ssortingng that is breaking correctly on words 

Merci à suitianshi, voici ma solution.

  private Ssortingng getWidthFitSsortingng(Ssortingng input) { Paint paint = text.getPaint(); // you can define max width by yourself int maxWidth = getContentMaxWidth(); float width = paint.measureText(input); if (width > maxWidth) { List words = Arrays.asList(input.split("\\s")); int breakLinePosition = 0; Ssortingng toBreakLineText; List toBreakLineWords = new ArrayList<>(); while (breakLinePosition < words.size()) { toBreakLineWords.add(words.get(breakLinePosition)); toBreakLineText = TextUtils.join(" ", toBreakLineWords); float currentWidth = paint.measureText(toBreakLineText); if (currentWidth > maxWidth) { break; } breakLinePosition ++; } if (breakLinePosition > 1) { toBreakLineWords.remove(toBreakLineWords.size() - 1); toBreakLineText = TextUtils.join(" ", toBreakLineWords); List fromBreakLineWords = new ArrayList<>(); for (int i = breakLinePosition; i < words.size(); i++) { fromBreakLineWords.add(words.get(i)); } return toBreakLineText + "\n" + getWidthFitString(TextUtils.join(" ", fromBreakLineWords)); } else { return input; } } return input; } 

Cela a fonctionné et nettoyé le code.