Android 에 서 는 TextView 가 고정된 줄 수 를 초과 하여"...모두 펼 치기"를 표시 합 니 다.
TextView 에 한 줄 만 표시 하고 여러 부분 을 생략 번호 로 대체 하려 면 xml 파일 에 있 는 TextView 컨트롤
android:singleLine="true"만 추가 하면 된다 는 것 을 잘 알 고 있 습 니 다.여러 줄 을 표시 하려 면 어떻게 해 야 합 니까?잔말 말고 효과 도 를 먼저 보 세 요.
펼 치기 전:
 
 펼 친 후:
 
 예제 코드:
도구 종류:
 /**
 *   textView  ...          
 * @param context    
 * @param textView textview
 * @param minLines      
 * @param originText    
 * @param endText     
 * @param endColorID       id
 * @param isExpand          
 */
 public void toggleEllipsize(final Context context,
    final TextView textView,
    final int minLines,
    final String originText,
    final String endText,
    final int endColorID,
    final boolean isExpand) {
 if (TextUtils.isEmpty(originText)) {
  return;
 }
 textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
  .OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
  if (isExpand) {
   textView.setText(originText);
  } else {
   int paddingLeft = textView.getPaddingLeft();
   int paddingRight = textView.getPaddingRight();
   TextPaint paint = textView.getPaint();
   float moreText = textView.getTextSize() * endText.length();
   float availableTextWidth = (textView.getWidth() - paddingLeft - paddingRight) *
    minLines - moreText;
   CharSequence ellipsizeStr = TextUtils.ellipsize(originText, paint,
    availableTextWidth, TextUtils.TruncateAt.END);
   if (ellipsizeStr.length() < originText.length()) {
   CharSequence temp = ellipsizeStr + endText;
   SpannableStringBuilder ssb = new SpannableStringBuilder(temp);
   ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor
     (endColorID)),
    temp.length() - endText.length(), temp.length(),    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
   textView.setText(ssb);
   } else {
   textView.setText(originText);
   }
  }
  if (Build.VERSION.SDK_INT >= 16) {
   textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  } else {
   textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  }
  }
 });
 }예:
boolean isExpandDescripe = false;//        false,    ;
Onclick() {
...
  //  textView      :
  case R.id.tv_info_node_describe:
  if (isExpandDescripe) {
   isExpandDescripe = false;
   tv_info_node_describe.setMaxLines(2);//   
  } else {
   isExpandDescripe = true;
   tv_info_node_describe.setMaxLines(Integer.MAX_VALUE);//   
  }
  textViewSpanUtil.toggleEllipsize(context,
   tv_info_node_describe, 2,
   text,
   "    ",
R.color.gray_discovery_ababab, isExpandDescripe);
  break;
...
}이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가치 가 있 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.