【问题标题】:How to prevent text from exiting the screen如何防止文本退出屏幕
【发布时间】:2021-12-28 17:50:47
【问题描述】:

我的问题很简单:如果我有一个文本视图并且文本太长,它刚刚超过了屏幕限制,我怎样才能让它分成多行以便它适合屏幕

p>
<TextView
        android:id="@+id/movie_cast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/movie_image"
        app:layout_constraintTop_toBottomOf="@id/movie_rating_bar"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="@color/black"
        android:textSize="12sp"
        tools:text="Cast: Ryan Reynolds,Dwayne Johnson,Gal Gadot"/>

另外,为什么某些文本适合我的模拟器屏幕,而当我在手机上运行应用程序时它不适合,为什么它没有相应地调整大小。看看文本是如何从屏幕上消失的继续下一行

【问题讨论】:

  • 它会自动分割文本,除非你设置了 singleLine 属性。您可能想发布您的 xml
  • 不拆,我马上贴出来
  • 是不是在模拟器/设备上运行应用时不拆分,还是在设计视图中不拆分?
  • 没有在模拟器和设备上拆分。显然它在设计视图中拆分,但只有在它通过几个词超过屏幕限制之后,而不是在达到限制后立即
  • 您已将TextView 宽度设置为wrap_content,因此它会延伸到屏幕边缘之外(它并不关心,您没有将其限制在此范围内)尽可能宽以适合一行中的文本。这就是wrap_content 所做的 - 尽可能大地伸展。您不希望这样 - 您想通过设置约束来限制宽度(即,将末端约束到父级末端,将边缘锁定到屏幕边缘)

标签: android kotlin split textview


【解决方案1】:

看起来您正在使用 ConstraintLayout ,而不是 android:layout_width="wrap_content" 使用 android:layout_width="0dp" 并根据约束修复 startend 的布局。

【讨论】:

  • 这会根据屏幕大小适合文本吗?
  • @DragosCiupe 是的,因为它不会尝试将全文包装在 textview 中,它可能会为溢出测试添加行,添加 android:single_line="true" &amp;&amp; android:wllipsize="end"
【解决方案2】:

将此属性添加到文本视图

 app:layout_constrainedWidth="true" 

喜欢这个

<TextView
        android:id="@+id/movie_cast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"  // this line 
        app:layout_constraintStart_toEndOf="@id/movie_image"
        app:layout_constraintTop_toBottomOf="@id/movie_rating_bar"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="@color/black"
        android:textSize="12sp"
        tools:text="Cast: Ryan Reynolds,Dwayne Johnson,Gal Gadot"/>

【讨论】:

    【解决方案3】:

    您限制了视图的开始,但没有限制视图的结束。也约束结束,所以它知道何时结束 textview。没有对末尾的约束,并且使用 wrap_content 长度,它将使文本视图尽可能大,以适应所有文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多