2017-09-18 5 views
0

String의 마지막 부분을 표시하고자하는 TextView이 있습니다. 일반적으로 TextView에 큰 텍스트를 설정하면 텍스트의 첫 번째 부분은 TextView에 표시됩니다. 하지만 내 요구 사항은 마지막 부분을 표시하는 것입니다 (마지막 전체 정지는 TextView의 왼쪽에 표시됩니다). 텍스트 크기를 모르지만 크기는 커집니다. 한 줄만 사용해야하고 타원 (3 점)을 사용하고 싶지 않습니다. 이 밖에TextView에서 텍스트의 마지막 부분을 표시하는 방법

답변

2

확인,

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); 
관련 문제