【问题标题】:EditText inside ConstraintLayout not working properlyConstraintLayout 中的 EditText 无法正常工作
【发布时间】:2019-12-26 16:04:28
【问题描述】:

我有一个AppCompatEditText 作为搜索栏和一个FrameLayout 作为“清除”按钮,都在ConstraintLayout 内:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ...
    android:layoutDirection="ltr">

    <FrameLayout
        android:id="@+id/clearBtnLyt"
        android:layout_width="36dp"
        android:layout_height="36dp"
        ...
        app:layout_constraintBottom_toBottomOf="@+id/editText1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/editText1">

        ... CONTENTS ...

    </FrameLayout>

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ...
        android:layoutDirection="rtl"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/clearBtnLyt"
        app:layout_constraintTop_toBottomOf="@id/toolbar1" />

    ...
</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintLayout 设置为从左到右,只有AppCompatEditText 是 RTL。这些视图在 Android Studio 的布局预览中得到了很好的展示。

但当我在装有 Android 8.0 操作系统的三星 Galaxy A5 (2017) 上运行该应用程序时,它看起来像这样:

我无法在AppCompatEditText 中输入任何内容或单击它,但“清除”按钮正在工作。我的手机也设置为从左到右。 我做错了什么?

【问题讨论】:

  • 这里需要xml
  • @pankaj-kumar 我还写了ConstraintLayout 代码。请检查它。
  • 为什么不把AppCompatEditText的layout_width设置成“match_parent”呢?
  • 因为我们不能在ConstraintLayout 中使用match_parent。我们只能将约束和这些值用于ConstraintLayout 的内容的layout_widthlayout_height 属性:0dp (match_constraint)、wrap_content 或自定义值。

标签: android android-layout view android-edittext android-constraintlayout


【解决方案1】:

我认为这是一个错误,当您更改 layoutDirection RTL 时,开始/结束和左/右之间存在混淆, 你可以把 start 改成 left 和 right 改成 en,同理:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/editText1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    ...
    android:layoutDirection="rtl"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/clearBtnLyt"
    app:layout_constraintTop_toBottomOf="@id/toolbar1" />

【讨论】:

    【解决方案2】:

    问题是因为我将AppCompatEditTextlayoutDirection 设置为rtl。因为它应该是从右到左的,所以我没有将其设置为ltr,但我将其约束从

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/clearBtnLyt"
        app:layout_constraintTop_toBottomOf="@id/toolbar1"
    

        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/clearBtnLyt"
        app:layout_constraintTop_toBottomOf="@id/toolbar1"
    

    请注意,我将 End 更改为 Right 并将 Start 更改为 Left,例如app:layout_constraintStart_toEndOfapp:layout_constraintLeft_toRightOf

    【讨论】:

      猜你喜欢
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 2016-09-11
      相关资源
      最近更新 更多