【问题标题】:Strange constrained_width behaviour with TextViewTextView 的奇怪 constrained_width 行为
【发布时间】:2021-01-26 23:01:05
【问题描述】:

基本上我有一个如图所示的布局。我正在使用指南来限制蓝色框的宽度。这意味着 ConstraintLayout 本身具有全屏宽度,蓝色框具有其layout_constraintEnd_toStartOf="@id/guideline"

蓝色框只是带有文本的TextView的背景颜色,没有任何额外的容器或任何东西。

为了让蓝色盒子尊重其宽度计算的约束,我必须在盒子上设置layout_constrainedWidth="true"。如您所见,宽度随后受到限制,但问题是 wrap_content 之后无法正常工作。

第一个框显示,如果文本只有一行,则行为符合预期,但如果文本跨越多行(如在第二个框中)wrap_content 中断并且框始终为“全宽”(开始屏幕直到指南)。

有人遇到过类似的事情吗?这是一个错误还是我误解了什么。右侧的灰色框可以观察到相同的损坏行为。

我认为这与 TextView 的破坏行为有关,但是否有修复或解决方法?据我所知,在 ConstraintLayout 之外,宽度将等于文本,所以我认为这是一个错误。

编辑 XML 代码: 这是“气泡”之一的基本代码,如前所述,我希望视图与文本一样宽。

<?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">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_message_history_receiver_background"
        android:fontFamily="@font/nunito_semibold"
        android:includeFontPadding="false"
        android:paddingStart="15dp"
        android:paddingTop="10dp"
        android:paddingEnd="15dp"
        android:paddingBottom="10dp"
        android:textColor="#FFFFFF"
        android:textSize="17sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideline"
        app:layout_constraintStart_toEndOf="@id/profile_image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintWidth_max="wrap"
        tools:text="Just testing something, somehowthisisnotworking" />

    <app.jooy.messenger.ui.components.generic.profile_image.ProfileImage
        android:id="@+id/profile_image"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginEnd="10dp"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintVertical_bias="1"
        app:backgroundColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.85" />

</androidx.constraintlayout.widget.ConstraintLayout>

这里是这个特定 xml 代码的输出:

【问题讨论】:

  • 我认为你希望如果文本长度超过那么它应该在指南之前,但如果它小于那么它应该是一个换行。因此,对于单行,它工作正常,但对于多行,它需要直到指导。尝试添加此属性。 app:layout_constraintWidth_max="wrap"
  • @KishanMaurya 谢谢你的回答。不幸的是,您添加 app:layout_constraintWidth_max="wrap" 的建议没有改变任何东西。你有什么其他想法我可以尝试吗?
  • 研究问题所需的 XML 代码。
  • @rahat 我添加了“泡泡”的基本xml代码
  • @MaxGierlachowski 您希望图像加上文本视图的宽度覆盖父级宽度的 80%,对吧?

标签: android view android-constraintlayout


【解决方案1】:

试试这个,根据需要调整边距和填充。并根据您的需要更改guideandroid:layout_marginBottom

<?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:paddingStart="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:includeFontPadding="false"
        android:paddingStart="15dp"
        android:paddingTop="10dp"
        android:paddingEnd="15dp"
        android:paddingBottom="10dp"
        android:textColor="#FFFFFF"
        android:textSize="17sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guide"
        app:layout_constraintWidth_percent=".8"
        tools:text="Just testing something, Some how this is not working" />

     <app.jooy.messenger.ui.components.generic.profile_image.ProfileImage
        android:id="@+id/profile_image"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginStart="5dp"
        app:backgroundColor="@color/colorPrimary"
        app:layout_constraintStart_toStartOf="@id/text"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/guide"
        android:layout_width="0dp"
        android:layout_height="0.2dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/profile_image"
        app:layout_constraintVertical_bias="1"
        app:layout_constraintTop_toTopOf="@id/profile_image" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多