點擊查看全文, 展開全部內容 TextView

<TextView
    android:id="@+id/authorMessage"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="12dp"
    android:layout_marginRight="16dp"
    android:ellipsize="end"
    android:maxLines="3"
    android:lineSpacingMultiplier="1.2"
    android:text="XXXXXXXXXXXXXXXXXXXXXXXX"
    android:textColor="#645441"
    android:textSize="14dp" />

<TextView
    android:id="@+id/authorMessageMore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="0dp"
    android:layout_gravity="bottom"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="點擊查看全部"
    android:textColor="#C44F20"
    android:textSize="20dp" />

 

TextView mAuthorMessage = findViewById(R.id.authorMessage);
TextView mAuthorMessageMore = findViewById(R.id.authorMessageMore);
Layout l = mAuthorMessage.getLayout();
if (l != null) {
   int lines = l.getLineCount();
   if (lines > 0) {
      if (l.getEllipsisCount(lines - 1) > 0) {
         sShowMore = true;
         mAuthorMessageMore.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v) {
                 //顯示TextView的全部內容其實就是取消這類隱藏文字的屬性
                 mAuthorMessage.setEllipsize(null);
                 mAuthorMessage.setSingleLine(false);
                 mAuthorMessageMore.setVisibility(View.GONE);
             }
          });
       }
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章