ConstraintLayout 尺寸问题
ConstraintLayout Sizing Issue
我在 ConstraintLayout
中遇到了儿童尺码问题。我有 3 个观点:
TextView
(item_quantity
):我希望它无限增长,但不要将 TextInputLayout
推离屏幕
TextInputLayout
(data_entry_layout
):我希望它填充所有可用的水平 space 并且最小宽度为 120dp
无论如何
ImageView
(camera_scan
): 这不会增长,只需要不被推出屏幕
如果 item_quantity
有短文本,我希望它看起来像下图。我希望 data_entry_layout
填满所有可用的 space。
如果 item_quantity
有长文本,我希望它填充和增长,但我希望 data_entry_layout
永远不会小于 120dp
。我想让它向 minWidth
致敬。所以,我希望它看起来像这样:
这是我当前的配置:
<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
的 layout_width
设置为 0dp
,那么 UI 可以处理大量文本,但会占用太多水平空间 space短文本:
如果我将 item_quantity
的 layout_width
设置为 wrap_content
,则 UI 可用于短文本,但会将 data_entry_layout
推离屏幕大文本:
我一定是遗漏了一些简单的东西。有没有办法让这两种情况都按照我的意愿进行?
与其使用 android:minWidth
属性,不如尝试使用 app:layout_constraintWidth_min
。在元素大小时,ConstraintLayout 似乎忽略或否决了 "android" 命名空间的某些属性。
我在 ConstraintLayout
中遇到了儿童尺码问题。我有 3 个观点:
TextView
(item_quantity
):我希望它无限增长,但不要将TextInputLayout
推离屏幕TextInputLayout
(data_entry_layout
):我希望它填充所有可用的水平 space 并且最小宽度为120dp
无论如何ImageView
(camera_scan
): 这不会增长,只需要不被推出屏幕
如果 item_quantity
有短文本,我希望它看起来像下图。我希望 data_entry_layout
填满所有可用的 space。
如果 item_quantity
有长文本,我希望它填充和增长,但我希望 data_entry_layout
永远不会小于 120dp
。我想让它向 minWidth
致敬。所以,我希望它看起来像这样:
这是我当前的配置:
<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
的 layout_width
设置为 0dp
,那么 UI 可以处理大量文本,但会占用太多水平空间 space短文本:
如果我将 item_quantity
的 layout_width
设置为 wrap_content
,则 UI 可用于短文本,但会将 data_entry_layout
推离屏幕大文本:
我一定是遗漏了一些简单的东西。有没有办法让这两种情况都按照我的意愿进行?
与其使用 android:minWidth
属性,不如尝试使用 app:layout_constraintWidth_min
。在元素大小时,ConstraintLayout 似乎忽略或否决了 "android" 命名空间的某些属性。