ConstraintLayout大小调整问题

时间:2020-04-01 16:58:09

标签: android android-constraintlayout

我在为ConstraintLayout中的孩子们调整大小时遇到​​问题。我有3个意见:

  • TextViewitem_quantity):我希望它无限增长,但不要将TextInputLayout推离屏幕
  • TextInputLayoutdata_entry_layout):我希望它填充所有可用的水平空间,并且无论什么情况,其最小宽度为120dp
  • ImageViewcamera_scan):这不会增长,只需​​要不被推离屏幕即可。

如果item_quantity包含短文本,我希望它看起来像下面的图像。我希望data_entry_layout填满所有可用空间。

short text version

如果item_quantity的文本很长,我希望它填充和增长,但是我希望data_entry_layout的大小不要小于120dp。我希望它能兑现minWidth。所以,我希望它看起来像这样:

long text version

这是我当前的配置:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <TextView
        android:id="@+id/item_quantity"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/data_entry_layout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="90,000 Boxes of Stuff Are Needed"
        android:textColor="@color/color_on_background"
        android:textSize="@dimen/workflow_item_view_item_quantity_text_size" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/data_entry_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/camera_scan"
        app:layout_constraintStart_toEndOf="@id/item_quantity"
        app:layout_constraintVertical_chainStyle="spread_inside"
        android:contentDescription="@string/disable_realwear_asr"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:minWidth="120dp"
        app:errorIconDrawable="@null">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/data_entry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/activity_instruction_data_entry_text_size"/>
    </com.google.android.material.textfield.TextInputLayout>

    <ImageView
        android:id="@+id/camera_scan"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/data_entry_layout"
        app:layout_constraintTop_toTopOf="@id/data_entry_layout"
        android:background="@android:color/transparent"
        android:clickable="true"
        android:foreground="@drawable/ripple_selector"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:src="@drawable/qrcode_scan"
        android:visibility="visible"
        tools:ignore="ContentDescription,KeyboardInaccessibleWidget" />
</androidx.constraintlayout.widget.ConstraintLayout>

如果我将item_quantity的{​​{1}}设置为layout_width,则UI可以处理大量文本,但是使用短文本会占用过多的水平空间:

0dp looks bad with short text

如果我将0dp的{​​{1}}设置为item_quantity,则UI可以使用短文本,但是可以将layout_width移出大文本:

wrap_content looks bad with long text

我一定缺少简单的东西。我有办法让这两种情况都做我想做的事吗?

1 个答案:

答案 0 :(得分:2)

请尝试使用android:minWidth,而不要使用app:layout_constraintWidth_min属性。当涉及到元素大小调整时,ConstraintLayout似乎忽略或否决了“ android”命名空间的某些属性。