【问题标题】:How to size textview to not go outside the screen如何调整textview的大小以不超出屏幕
【发布时间】:2020-12-27 14:30:55
【问题描述】:

我必须实现一个聊天机器人,但我不知道如何为消息调整文本视图的大小。

我现在拥有的是这个

我希望气球完全适合文本,右边没有白色空白,不使用maxWidth

我的代码是

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/bot_icon"
        android:layout_width="50dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:src="@drawable/ic_botchat"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@id/bot_icon"
        app:layout_constraintTop_toBottomOf="@id/bot_icon">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/textMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:background="@drawable/bot_message"
            android:elevation="3dp"
            android:fontFamily="@font/roboto"
            android:paddingStart="16dp"
            android:paddingTop="10dp"
            android:paddingEnd="16dp"
            android:paddingBottom="10dp"
            android:textColor="@color/visia"
            android:textSize="18sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

我尝试用wrap_content修改AppCompatTextView中的android:layout_width,但是文本显示在一行中,超出了屏幕。

【问题讨论】:

  • 您需要以这种方式设计您的 .9.png 图像(背景气泡)。

标签: android xml kotlin textview chat


【解决方案1】:

根据this的回答,你不能通过使用默认的TextView来实现你想要的,你需要有一个自定义的文本视图,你可以覆盖onMeasure方法,这样最大宽度就会是你最大的线的宽度。

【讨论】:

    【解决方案2】:

    使用属性“maxLines”来指定你的文本视图应该有的行数。例如,

    android:maxLines="2"
    

    【讨论】:

    • 这不是我问的
    【解决方案3】:

    尝试为文本父级设置宽度并放置文本 wrap_content

    【讨论】:

      【解决方案4】:

      如果您的问题是关于如何删除 textView 中的正确空格,只需删除 textview 属性中的这一行:

      app:layout_constraintEnd_toEndOf="parent"
      

      实际上,您将 textview 的末尾绑定到其父节点的末尾,这就是 wrap_content 无法正常运行的原因。所以结果是:

      <?xml version="1.0" encoding="utf-8"?>
      
      <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      xmlns:app="http://schemas.android.com/apk/res-auto">
      
      <androidx.appcompat.widget.AppCompatImageView
          android:id="@+id/bot_icon"
          android:layout_width="50dp"
          android:layout_height="0dp"
          android:adjustViewBounds="true"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          android:src="@drawable/boy_icon"
          android:layout_marginStart="8dp"
          android:layout_marginTop="16dp"
          />
      
      <androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintStart_toEndOf="@id/bot_icon"
          app:layout_constraintTop_toBottomOf="@id/bot_icon">
      
          <androidx.appcompat.widget.AppCompatTextView
              android:id="@+id/textMessage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="8dp"
              android:background="@drawable/round_gray_20"
              android:elevation="3dp"
              android:paddingStart="16dp"
              android:paddingTop="10dp"
              android:paddingEnd="16dp"
              android:paddingBottom="10dp"
              android:text="fsdfsdfsdfsdsfsd "
              android:textColor="@color/blackText"
              android:textSize="18sp"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      请注意,我可能更改了一些drawables 并使用我的来查看结果。所以相应地将它们还原为原始的。

      【讨论】:

      • 不管用 android:layout_width="0dp" 和 android:layout_width="wrap_content"
      • 正如我在布局编辑器中测试的那样,它绝对有效。删除app:layout_constraintEnd_toEndOf="parent"后究竟是什么问题?
      • 我从我发布的代码中删除了结束约束,并更改了气球右侧的边距现在消失了(气球附在屏幕的右侧)。我想要的是“per”右侧气球的白色部分消失,以便气球正好在行尾结束
      • 我编辑了我的答案并添加了xml 代码,我得到了正确的结果表单。正是使用这个xml 并告诉结果。
      • 好的,但请尝试使用此文本“您想要个性化哪个橱柜?”。你会看到在“to”的右边有一个空白区域
      【解决方案5】:

      将您的布局编辑成这样

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#D7D7D7">
      
          <androidx.appcompat.widget.AppCompatImageView
              android:id="@+id/bot_icon"
              android:layout_width="50dp"
              android:layout_height="0dp"
              android:layout_marginStart="8dp"
              android:layout_marginTop="16dp"
              android:adjustViewBounds="true"
              android:src="@drawable/ic_botchat"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <androidx.appcompat.widget.AppCompatTextView
              android:id="@+id/textMessage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="8dp"
              android:layout_marginBottom="8dp"
              android:background="@drawable/bot_message"
              android:elevation="3dp"
              android:fontFamily="@font/roboto"
              android:maxLines="2"
              android:paddingStart="16dp"
              android:paddingTop="10dp"
              android:paddingEnd="16dp"
              android:paddingBottom="10dp"
              android:text="Lorem Ipsum."
              android:textColor="@color/visia"
              android:textSize="18sp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.0"
              app:layout_constraintStart_toEndOf="@id/bot_icon"
              app:layout_constraintTop_toBottomOf="@id/bot_icon" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      然后创建 XML 文件“bot_message.xml”

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <corners
              android:bottomLeftRadius="20dp"
              android:bottomRightRadius="20dp"
              android:topLeftRadius="0dp"
              android:topRightRadius="20dp" />
      
          <solid android:color="#F5F5F5" />
      
      </shape>
      

      【讨论】:

        猜你喜欢
        • 2011-09-01
        • 2021-09-25
        • 2016-04-20
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 2011-05-08
        相关资源
        最近更新 更多