Поместите кнопку и найдите панель в контейнере - PullRequest
0 голосов
/ 15 апреля 2020

Я пытаюсь получить панель поиска и две мои кнопки в поле, которое находится под окном поиска. (Я нарисовал черным там, где я пытаюсь разместить панель поиска и кнопку). Я прикрепил изображение, чтобы лучше объяснить, как бы я хотел, чтобы мой макет выглядел. Я попытался изменить макет фрейма и добавить ограничения, но я не могу понять, что я делаю неправильно. Каждый раз, когда я добавляю ограничения и перемещаю одну кнопку 1, все остальные группы объединяются в верхнем левом углу. Является ли их конкретная c раскладка, которую я должен использовать, чтобы получить мою панель поиска в этом поле и мои 2 кнопки в правом нижнем углу? image Вот мой код:

<?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">

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/searchButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/searchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="@string/form_search_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <FrameLayout
        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="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchButton"
        app:layout_constraintVertical_bias="0.0">

        <Button
            android:id="@+id/stopButton"
            android:layout_width="97dp"
            android:layout_height="38dp"
            android:layout_marginStart="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginBottom="18dp"
            android:hint="Stop"
            android:text="@string/stop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/pauseButton"
            android:layout_width="85dp"
            android:layout_height="34dp"
            android:hint="pause"
            android:text="@string/pause" />

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="378dp"
            android:layout_height="12dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="123dp" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Ответы [ 2 ]

1 голос
/ 15 апреля 2020

Вы можете попробовать использовать ConstraintLayout, заменяя FrameLayout,

Ограничение Seekbar:

  • от начала до конца родительского
  • end to конец родительского элемента

Затем используйте LinearLayout для удержания двух кнопок.

Затем ограничьте LinearLayout:

  • снизу до основания родительского элемента
  • конец к концу родительского элемента

В результате этого панель поиска останется в верхнем правом углу, а кнопки - в правом нижнем углу.

1 голос
/ 15 апреля 2020

Я предполагаю, что под окном поиска вы имеете в виду SeekBar и 2 кнопки в одной строке, но я не могу понять ваш рисунок.

Вы должны использовать LinearLayout для этого.

1) Если Вы хотите, чтобы все 3 в одной строке, тогда вы должны сделать это

<LinearLayout
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_vertical">

    <androidx.appcompat.widget.AppCompatSeekBar
            android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="wrap_content"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content" android:layout_height="wrap_content"/>

    <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</LinearLayout>

2) Если вы хотите, чтобы панель поиска находилась в одной строке и на 2 кнопки ниже этой панели поиска, но в одной строке, то

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent" android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatSeekBar
            android:layout_width="match_parent" android:layout_height="wrap_content"/>

    <LinearLayout
            android:orientation="horizontal"
            android:weightSum="2"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:gravity="center_vertical">

        <com.google.android.material.button.MaterialButton
                android:layout_weight="1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>

        <com.google.android.material.button.MaterialButton
                android:layout_weight="1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

Вы можете указать ширину c для кнопок, если это возможно. Пожалуйста, игнорируйте AppCompatSeekBar & Material Button. Он будет работать с SeekBar & Button таким же образом.

Вы также можете поместить 2 кнопки в нижней части экрана, если вам нужно, тогда родительский элемент SeekBar & LinearLayout (состоит из 2 кнопок) должен быть Relative Layout с Высота соответствия родительского и LinearLayout (состоит из 2 кнопок) должна выровнять родительское основание.

ОБНОВЛЕНИЕ:

Здесь, как вы и хотели. Он может немного отличаться, но вы можете изменить его сейчас.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/containerSearch"
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_margin="16dp">

        <EditText
            android:id="@+id/searchEditText"
            android:layout_weight="1"
            android:layout_width="0dp" android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toStartOf="@+id/searchButton"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginEnd="8dp"/>

        <Button
            android:id="@+id/searchButton"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginStart="8dp"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:weightSum="5"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/containerSearch"
        android:layout_margin="16dp">

        <RelativeLayout
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_weight="3"
            android:layout_width="0dp" android:layout_height="12dp"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="end"
        android:layout_margin="24dp"
        android:weightSum="5"
        app:layout_constraintBottom_toBottomOf="parent">

        <RelativeLayout
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:orientation="horizontal"
            android:weightSum="2"
            android:layout_weight="3"
            android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_margin="16dp">

            <Button
                android:id="@+id/pauseButton"
                android:text="PAUSE"
                android:layout_weight="1"
                android:layout_width="0dp" android:layout_height="40dp"
                android:hint="pause" 
                android:layout_marginEnd="8dp"/>

            <Button
                android:id="@+id/stopButton"
                android:text="STOP"
                android:layout_weight="1"
                android:layout_width="0dp" android:layout_height="40dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="8dp"/>

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Теперь, если вам нужно показать список или другие элементы управления между вашим SeekBar и нижними кнопками, тогда эти два должны быть внутри единого LinearLayout, где контейнер SeekBar это первый ребенок, а ваш список - второй, а нижние кнопки - у третьего ребенка. или вы можете удерживать нижние кнопки на BottomBar, который остается сверху.

...