Как создать перекрывающиеся элементы в Android? - PullRequest
0 голосов
/ 04 мая 2018

Я пытаюсь реализовать экран, похожий на этот:

enter image description here

Я могу реализовать вышеизложенное. Я использовал следующую логику:

FrameLayout
  |- Linear Layout
    |- Button (Purple Colour, Layout Width = 1)
    |- Button (Blue Colour, Layout Width = 1)
  |- Linear Layout (Layout_Gravity = center)
    |- Button (Button Text)

Исходный код

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/accent_material_light"
        android:orientation="vertical"
        android:padding="0dp"
        android:weightSum="100">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="40"
            android:background="@android:color/holo_purple" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:background="@android:color/holo_blue_light" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@color/abc_search_url_text_pressed">

        <Button
            android:id="@+id/button2"
            android:layout_height="48dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent" />
    </LinearLayout>


</FrameLayout>

Вопрос: -

Как разместить кнопку на границе двух видов, если два вида (фиолетовый и синий) должны занимать 40% и 60% экрана соответственно. Я не могу использовать layout_gravity для центрирования в этом случае. Как решить эту проблему?

Это выглядит примерно так: -

enter image description here

Как решить эту проблему?

Ответы [ 2 ]

0 голосов
/ 04 мая 2018

Для повышения производительности необходимо уменьшить иерархию. Здесь вместо двух / трехуровневой иерархии вы можете использовать единственный макет ограничения в качестве корневого макета.

В макете ограничений используется кнопка с ограничением

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

это установит кнопку в центре, после чего установите два изображения

imageView1:

 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintBottom_toTopOf="id:button"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

imageView2:

 app:layout_constraintTop_toBottomOf="id:button"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"

Многоуровневая иерархия занимает много времени для загрузки пользовательского интерфейса. Итак, попробуйте уменьшить его.

0 голосов
/ 04 мая 2018

Более простое решение может быть ConstraintsLayout. ниже приведено решение с комбинацией линейного и относительного расположения.

<RelativeLayout 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="wrap_content">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorPrimaryDark"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_purple"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:gravity="center">

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Install" />
</RelativeLayout>
...