【问题标题】:Constraint Layout's match_constraints is not working properly约束布局的 match_constraints 无法正常工作
【发布时间】:2020-07-11 10:14:31
【问题描述】:

我有一个双重约束布局,一个是占据所有屏幕的框,第二个包含一些视图(特别是 editTextview)。我希望我的 editText 占据右侧的所有剩余空间,如下图所示

我已经把 layout_width = 0dp 来达到这个目的,但它不起作用。事实上,editText 似乎要短得多;我仍然可以写数字,但它们都保留在那部分空间中(它显示了 editText 部分中的最后 6 个数字)。

此外,当我停止打字并且键盘消失时,我之前写的所有数字都会在右侧展开,如您在此处看到的:

我尝试使用 layout_width = 500dp 或类似方法解决问题,但最终结果不是我想要达到的。我希望能够为所有行写数字,当我到达它时,第一个数字必须从视图中消失,新写的数字必须出现在右侧(我的意思是,它必须显示最后 30 个字符示例)。

这里是有问题的布局部分:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/boxEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="@drawable/layout_border"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tabLayout">

    <TextView
        android:id="@+id/buttonPrefix"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="+39"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/boxEditText"
        app:layout_constraintTop_toTopOf="parent"></TextView>

    <View
        android:id="@+id/verticalLine"
        android:layout_width="1dp"
        android:layout_height="25dp"
        android:layout_margin="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/black"
        app:layout_constraintBottom_toBottomOf="@+id/buttonPrefix"
        app:layout_constraintStart_toEndOf="@+id/buttonPrefix"
        app:layout_constraintTop_toTopOf="@+id/buttonPrefix" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/edit_text_border"
        android:hint="@string/Telefono"
        android:inputType="phone"
        android:maxLines="1"
        app:layout_constraintBottom_toBottomOf="@+id/verticalLine"
        app:layout_constraintStart_toEndOf="@+id/verticalLine"
        app:layout_constraintTop_toTopOf="@+id/verticalLine" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-xml android-constraintlayout


    【解决方案1】:

    我不确定哪些图片显示有效,哪些显示无效示例,但我注意到了这一点:

    您忘记了结束约束: app:layout_constraintEnd_toEndOf="parent"

    如果文本与您的背景重叠,您可能可以添加一些填充: android:paddingEnd="16dp"

    【讨论】:

      【解决方案2】:

      如果我理解的很好,你可以写

      android:gravity="right"
      

      这样,您将从右侧开始输入,并且随着您继续输入,第一个数字将移至左侧。现在,如果您希望显示特定数量的字符,您可以通过更改数字的大小来实现。

      【讨论】:

        猜你喜欢
        • 2015-04-23
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 2023-01-16
        相关资源
        最近更新 更多