ConstraintLayout,并且高度大于或等于

问题描述 投票:0回答:2

我想要实现的目标非常简单,但是我无法输出它。

我想要一个简单的布局,带有固定宽度和高度的图标,并在右侧显示一个文本。很容易。

我也想相对于图标垂直居中放置文本。这也很容易。

当文本很短并且停留在一行时,一切都是完美的。当文本较长时,就会出现问题。

使用此代码:

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_icon"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. "
        app:layout_constraintHeight_min="48dp"
        app:layout_constraintLeft_toRightOf="@id/icon"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/icon" />
</androidx.constraintlayout.widget.ConstraintLayout>

根据需要显示一行文本,溢出48dp时将隐藏更长的文本

代码和据我所知有什么问题?

谢谢

android android-layout android-constraintlayout
2个回答
0
投票

检查此:

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

        <ImageView
            android:id="@+id/icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_icon"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. "

            app:layout_constraintHeight_min="48dp"
            app:layout_constraintLeft_toRightOf="@id/icon"
            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>

您可以使用TextViewandroid:drawableStart属性


0
投票

我认为这不是使用TextView的好方法。我建议像这样使用TextView:

android:layout_width =“ wrap_content”

android:layout_height =“ wrap_content”

android:textSize =“ 20sp”(所需的textSize)

app:layout_constrainedWidth =“ true”

受约束的宽度告诉textView遵守其Start和End约束。

© www.soinside.com 2019 - 2024. All rights reserved.