J'ai un TextView
dans lequel je veux montrer la dernière partie d'un String
. Normalement, si je mets un grand texte en TextView
la première partie du texte sera affichée en TextView
. Mais mon exigence est de montrer la dernière partie (Dernier arrêt complet sera montré à gauche de TextView
). Je ne connais pas la taille du texte, mais elle sera grande. Je dois utiliser une seule ligne et je ne veux pas utiliser d'ellipses (3 points).Comment afficher la dernière partie du texte dans TextView
0
A
Répondre
2
Vérifiez ce,
TextView tvTextView;
String text = "YOUR LONG TEXT WILL BE HERE", subString = "",
int textViewWidth = 100;
int numChars;
tvTextView.setText(text);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
textViewWidth = displayMetrics.widthPixels; // you need to subtract paddings and margins if needed
Log.d("textview_width", "width " + textViewWidth + " length " + text.length());
Paint paint = tvTextView.getPaint();
for (numChars = 1; numChars <= text.length(); ++numChars) {
if (paint.measureText(text, 0, numChars) >= textViewWidth) {
break;
}
}
subString = text.substring(text.length() + 1 - numChars, text.length());
int dots = subString.split("\\.").length * 2;
if(text.length() <= numChars - 1){
subString = text;
}
else {
subString = text.substring(text.length() + 1 + dots - numChars, text.length());
}
tvTextView.setText(subString);