【问题标题】:Using wrap_content behavior and ellipsize if text too long如果文本太长,则使用 wrap_content 行为和省略号
【发布时间】:2020-02-10 13:25:01
【问题描述】:

我的布局上有三个视图 TextView1 TextView2ImageView

TextView1 包裹内容,而 TextView2TextView1 的末尾对齐

TextView1 TextView2 ImageView TextView1 TextView1 ImageView

TextView1 TextView1 > 一起填满了布局的所有宽度

我已经尝试了很多选项来使用 LinearLayout、RelativeLayout、ConstraintLayout 在 xml 中创建它,但它们都没有给我想要的结果。

最近的是

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

        <TextView
            android:id="@+id/card_item_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:gravity="start"
            app:layout_constraintWidth_max="284dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="WWWWWWWWWWWWWWWWWWWWWWW"/>

        <TextView
            android:id="@+id/card_item_small_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/offset_6"
            android:paddingEnd="@dimen/offset_6"
            android:maxLines="1"
            app:layout_constraintStart_toEndOf="@id/card_item_name"
            app:layout_constraintEnd_toStartOf="@id/card_item_bank_logo"
            app:layout_constraintTop_toTopOf="parent"
            android:gravity="start"
            tools:text="**** 5555"/>

        <ImageView
            android:id="@+id/card_item_bank_logo"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/offset_24"
            android:maxHeight="@dimen/offset_24"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@drawable/ic_language"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

这里的关键在于 TextView1 属性

android:layout_width="0dp"
app:layout_constraintWidth_max="284dp"

但这意味着我必须在运行时计算 layout_constraintWidth_max 值,因为 TextView2ImageView 的宽度不是固定的,取决于它的价值观

有没有办法只在没有“魔术”数字的 xml 中制作它?

【问题讨论】:

  • 你试过为 textview1 设置约束结束吗?所以 tv1 endToStart 的 tv2 并删除最大宽度
  • @P.Juni 是的,我有。在这种情况下,TextView1 将 match_constraint 而不是 wrap_contant。这会导致 TextView1 填充所有可用空间

标签: android android-layout


【解决方案1】:

试试 安卓:layout_weight="1" 您可以解决此问题以适应视图上的所需宽度。 为使这项工作发挥作用,请使用水平方向的 LinearLayout。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多