【问题标题】:ConstraintLayout, TextView and ImageView with Horizontal Alignment and Auto Sizing具有水平对齐和自动调整大小的 ConstraintLayout、TextView 和 ImageView
【发布时间】:2019-09-03 10:24:57
【问题描述】:

我一直面临一个问题,认为是一组多个不正确的定位选择,之前的一些信息:

1 - 可绘制图像的宽度和高度比它适合的要大。 2 - TextView 文本是动态的,但不应超过一行,如果超过一行,则应“剪切”。

我有以下代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/icon"
            android:text="my small text"
            android:lines="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/description"
            app:layout_constraintTop_toBottomOf="@id/title"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/icon"
            android:text="description text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/icon"
            app:layout_constraintStart_toEndOf="@id/title"
            app:layout_constraintTop_toTopOf="@id/title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/description"
            android:adjustViewBounds="true"
            android:scaleType="fitEnd"
            android:src="@drawable/ic_print"
            app:layout_constrainedWidth="true"
            app:layout_constrainedHeight="true"
            android:padding="8dp"
            android:layout_width="0dp"
            android:layout_height="0dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

一些解释:由于图像非常大,我需要将它定位在左侧,我使用 android:scaleType="fitEnd"android:padding="8dp" >.

结果令人满意:

问题:

当我在标题中放置非常大的文本时,我希望它在 ImageView 实际上将自身限制为 50% 时将其“推”到其极限。

当我实际使用“与 0dp 匹配的约束”时,就像我的 ImageView 有一个“固定”宽度

我已经尝试将 app:layout_constrainedWidth="true" 添加到 id:title 和 id:description(不成功)

【问题讨论】:

  • 看看约束布局中的障碍
  • @RohitSuthar 我会的

标签: android android-constraintlayout


【解决方案1】:

添加指南并限制您的标题,而不是将其限制在图标上。这样,当它到达指南时,它将停止增加,并且仍然受限于标题的图标将保持其最小空间。当 ellipsize 属性变得太大时,它会在标题末尾添加点。您需要做的就是通过调整指南上的百分比来确定图标的最小空间。

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

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="my small text"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toStartOf="@id/guideline"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="description text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/icon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:padding="8dp"
        android:scaleType="fitEnd"
        android:src="@drawable/ic_print"
        app:layout_constrainedHeight="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@id/description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/title"
        app:layout_constraintTop_toTopOf="@id/title" />

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


</androidx.constraintlayout.widget.ConstraintLayout>

在 cmets 中澄清后编辑: 这是建议的屏障解决方案。

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

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="my small text"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="description text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:src="@drawable/app_logo"
        app:layout_constraintBottom_toBottomOf="@id/description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/title"
        app:layout_constraintTop_toTopOf="@id/title" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
        app:constraint_referenced_ids="icon" />


</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 我认为有问题。文本覆盖了指南并且没有应用 EndToStart 限制。而且我不能一直根据 ImageView 调整指南(图标也是动态的,可以在各种分辨率下运行)
  • @Jonny 我误解了。如果图像也需要更加动态,我会按照 Rohit 的建议排除障碍
  • @Jonny 哪个视图优先,文本还是图标?哪个先调整大小?你想要原始图像,然后检查剩余空间并相应地调整文本,相反还是两者的某种组合?
  • 文本的优先级尽可能低,被图像占用(换行内容)。文字在左边,图片在右边。也就是说,如果我有大文本,它将一直显示到图像出现的最小宽度。
  • 我明白了,将 app:layout_constrainedHeight="true" 添加到描述中(用作高度参考约束)并将图像更改为 android:layout_width = "wrap_content" 并仅保留 app:layout_constrainedHeight= true ” 在里面。感谢您的努力。
猜你喜欢
  • 2013-07-24
  • 2013-03-08
  • 2020-03-25
  • 1970-01-01
  • 2017-12-03
  • 2011-08-31
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多