【问题标题】:Button Won't Move Next to EditText按钮不会移动到 EditText 旁边
【发布时间】:2020-07-11 13:18:54
【问题描述】:

我正在尝试将按钮放在我的编辑文本旁边,类似于您在 Google 上搜索内容时看到的搜索栏。我想移动搜索栏旁边右侧的按钮,但我不明白为什么 RelativeLayout 在这里不起作用。我阅读了 Stack 上发布的一些类似问题,并且答案说大多数都使用 RelativeLayout。我应该使用另一种类型的布局吗?

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/container1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">


        <EditText
            android:id="@+id/searchBox"
            android:layout_width="283dp"
            android:layout_height="46dp"
            android:layout_marginTop="8dp"
            android:autofillHints="@string/search"
            android:ems="10"
            android:inputType="textPersonName"
            android:visibility="visible" />

        <Button
            android:id="@+id/searchButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="@string/search" />

    </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-layout button android-edittext android-relativelayout


    【解决方案1】:

    在 Button 处添加此 Line 以添加约束

    android:layout_toEndOf="@+id/searchBox"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 2020-06-26
      • 2014-04-18
      相关资源
      最近更新 更多