Android TextView 限制顯示行數


限制TextView的顯示行數。
方法1:
android:maxLines="2"  //限制最大行數為2行

方法2:

android:lines="2"

 兩者之間的區別是:

  方法1:內容小於限制行數,只會占用內容需要的行數。

  方法2:內容小於限制行數,也會占用最大行數。

如果內容超出了最大行數那么就不會顯示,如果我們想在行末添加省略號來代替未被顯示的內容可以使用:

android:ellipsize="end"  //末尾省略號

實例:

<TextView
        android:id="@+id/MyTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="In android, TextView is a user interface control that is used to set and display the text to the user based on our requirements."
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

效果圖:內容需要占據4行

  

方法1:限制只能顯示兩行

<TextView
android:id="@+id/MyTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:maxLines="2"  //限制只能顯示兩行
android:text="In android, TextView is a user interface control that is used to set and display the text to the user based on our requirements."
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

效果圖:

  

 

 

 

方法2:限制只能顯示兩行

<TextView
        android:id="@+id/MyTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:lines="2"
        android:text="In android, TextView is a user interface control that is used to set and display the text to the user based on our requirements."
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

效果圖:和方法1的效果相同

  

 

如果內容小於兩行

方法1:

<TextView
        android:id="@+id/MyTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:maxLines="2"
        android:text="Hello"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

效果圖:內容只占需要的行數

  

 

 方法2:

<TextView
        android:id="@+id/MyTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:lines="2"
        android:text="Hello"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

效果圖:會占領限制的最大行數

  

 

 

 

 

 

參考:https://blog.csdn.net/qq_31403303/article/details/51506524


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM