RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1086036
Accepted
Anton Charov
Anton Charov
Asked:2020-02-22 20:17:18 +0000 UTC2020-02-22 20:17:18 +0000 UTC 2020-02-22 20:17:18 +0000 UTC

TextView 不会弄乱文本

  • 772

对于长文本,有些词超出范围。如何正确转账?taskView 中的长文本 = "Gjjjjfghfhgfhfhgfhgfhggfh fhghfgfgf gfhyyfgh fhfffgh fgfhhf fhhf hghg 不在房间内工作。" 根据转移,但它是不正确的。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="4dp"
    android:background="@drawable/customborder">

    <TextView
        android:id="@+id/rectangleNumber"
        android:layout_width="60dp"
        android:layout_height="28dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/rectangle"
        android:gravity="center_vertical|center_horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/dateTimeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/montserrat"
        app:layout_constraintStart_toStartOf="@+id/rectangleNumber"
        app:layout_constraintTop_toBottomOf="@+id/rectangleNumber" />

    <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/poppins_medium"
        app:layout_constraintStart_toStartOf="@+id/dateTimeView"
        app:layout_constraintTop_toBottomOf="@+id/dateTimeView" />

    <TextView
        android:id="@+id/cvalView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/poppins_medium"
        app:layout_constraintStart_toStartOf="@+id/address"
        app:layout_constraintTop_toBottomOf="@+id/address" />

    <TextView
        android:id="@+id/taskView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="4dp"
        android:fontFamily="@font/poppins_medium"
        app:layout_constraintStart_toStartOf="@+id/cvalView"
        app:layout_constraintTop_toBottomOf="@+id/cvalView" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/montserrat"
        app:layout_constraintStart_toStartOf="@+id/taskView"
        app:layout_constraintTop_toBottomOf="@+id/taskView" />

    <Button
        android:id="@+id/agreeBtn"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/rectangle"
        android:backgroundTint="#00CC4F"
        android:fontFamily="@font/montserrat"
        android:text="Принять"
        android:textColor="#FFFFFF"
        app:layout_constraintStart_toStartOf="@+id/description"
        app:layout_constraintTop_toBottomOf="@+id/description" />

    <Button
        android:id="@+id/disagreeBtn"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="#F23B51"
        android:text="Отклонить"
        android:textColor="#FFFFFF"
        android:fontFamily="@font/montserrat"
        android:background="@drawable/rectangle"
        app:layout_constraintLeft_toRightOf="@+id/agreeBtn"
        app:layout_constraintTop_toTopOf="@+id/agreeBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图像描述

android
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Sky
    2020-02-22T23:38:58Z2020-02-22T23:38:58Z

    为了让ConstraintLayout内的TextView能够正确显示长文本,首先必须将此组件绑定到容器的父边缘,或者最近的相邻元素,并将宽度值设置为 0dp android:layout_width="0dp。

    示例 #1:

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
    

    还有另一种选择。在这种情况下,我们只是将TextView的宽度拉伸到容器android:layout_width="match_parent"的全长。结果是一样的,文本换行就好了。但是,如果您决定将 2 个TextView组件放在同一行上,则此技巧将不起作用。

    示例 #2

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
    

    此外,我可以建议您使用ellipsize和maxLines等属性。如果文本长于给定值,它将在文本末尾添加一个省略号。

    示例#3:

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Lorem Ipsum является текст-заполнитель обычно используется в графических, печать и издательской индустрии для предварительного просмотра макета и визуальных макетах." />
    

    随附的屏幕截图解决您的问题:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        android:padding="8dp"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/rectangleNumber"
            android:layout_width="60dp"
            android:layout_height="28dp"
            android:layout_margin="4dp"
            android:background="#F59723"
            android:gravity="center"
            app:layout_constraintBottom_toTopOf="@+id/dateTimeView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="72877" />
    
        <TextView
            android:id="@+id/dateTimeView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:layout_constraintBottom_toTopOf="@+id/address"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rectangleNumber"
            tools:text="2019-12-28 09:00" />
    
        <TextView
            android:id="@+id/address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:layout_constraintBottom_toTopOf="@+id/cvalView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/dateTimeView"
            tools:text="Lorem Ipsum" />
    
        <TextView
            android:id="@+id/cvalView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:layout_constraintBottom_toTopOf="@+id/taskView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/address"
            tools:text="Lorem Ipsum?" />
    
        <TextView
            android:id="@+id/taskView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:layout_constraintBottom_toTopOf="@+id/description"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cvalView"
            tools:text="Lorem Ipsum! Это текст - рыба, часто используемый в печати и вэб-дизайне. Lorem Ipsum является стандартной рыбой для текстов на латинице с начала XVI века." />
    
        <TextView
            android:id="@+id/description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:layout_constraintBottom_toTopOf="@+id/agreeBtn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/taskView"
            tools:text="Это текст - рыба, часто используемый в печати и вэб-дизайне. Lorem Ipsum является стандартной рыбой для текстов на латинице с начала XVI века." />
    
        <Button
            android:id="@+id/agreeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:backgroundTint="#00CC4F"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/description"
            tools:text="Принять" />
    
        <Button
            android:id="@+id/disagreeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="#F23B51"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="@+id/agreeBtn"
            app:layout_constraintStart_toEndOf="@+id/agreeBtn"
            app:layout_constraintTop_toTopOf="@+id/agreeBtn"
            tools:text="Отклонить" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    在预览中显示结果

    Z.Y. 我建议您在码头更近一点地熟悉ConstraintLayout组件,另外您可以观看美丽的Rebecca Franks的有关此组件的视频,这是youtube频道上的视频。

    Z.Y.S. 我希望我解释清楚了一切,如果您有任何问题,请询问。

    • 2
  2. Alex_Skvortsov
    2020-02-22T20:27:04Z2020-02-22T20:27:04Z

    试试这样:

    <TextView
        android:id="@+id/taskView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:fontFamily="@font/poppins_medium"
        app:layout_constraintStart_toStartOf="@id/rectangleNumber"
        app:layout_constraintEnd_toEndOf="@+id/rectangleNumber"
        app:layout_constraintTop_toBottomOf="@+id/cvalView" />
    

    对标记的一般评论: 使用 ConstraintLayout 时,我建议您不要绑定到前一个元素,而是绑定到真正存在依赖关系的元素。也就是说,您为 rectangleNumber 设置了边距,然后我们将其余元素绑定到它,而不是相互绑定 - 这使得绑定更加明确。否则,将失去对什么缩进的理解。嗯,最好总是绑定至少三个边。

    • 0
  3. igo
    2020-02-23T03:23:46Z2020-02-23T03:23:46Z

    如果文本使用 /u0020 而不是空格(Unicode 字符 SPACE),则此字符不被视为空格,并且沿行的长度进行传输

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5