Подтвердить что ты не робот

Android: размещение ImageView при перекрытии между макетами

Я пытаюсь разместить ImageView в точке перекрытия между двумя макетами. На рисунке ниже моя цель состояла бы в том, чтобы разместить ImageView, где находится белый квадрат. ПРИМЕЧАНИЕ. Точка перекрытия не обязательно должна быть центрирована по вертикали, как показано ниже.

enter image description here

Возможно ли это в XML?

Мое единственное предположение прямо сейчас - сделать это в самом самом коде.

EDIT 8/3/2016: Для справки, я думаю, что ConstraintLayouts может быть лучшим будущим решением для этих типов проблем http://tools.android.com/tech-docs/layout-editor

4b9b3361

Ответ 1

Это сделает то, что вы хотите, с изображением фиксированной высоты или рассчитанным программно.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/layoutTop"
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/layoutTop" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/overlapImage"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_above="@id/layoutBottom"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-20dp" <!-- This should be always half the height, can also be calculated and added programtically -->
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Ответ 2

Попробуйте этот путь, надеюсь, что это поможет вам решить вашу проблему.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"
        android:layout_gravity="center"/>
</FrameLayout>

Ответ 3

использовать макет ограничения тогда предположим, что есть top_view и bottom_view Вы хотите разместить представление между top_view и bottom_view

<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"
android:orientation="vertical">
<LinearLayout
    android:id="@+id/top_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_size_100"
    android:background="@color/white"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/middle_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="@id/top_view"
    app:layout_constraintTop_toBottomOf="@id/top_view">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="abcd" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ipsum" />

</LinearLayout>

Это должно сделать это