首页 > 解决方案 > Android - 顶部带有按钮的拆分视图

问题描述



我正在尝试在 android 中拆分屏幕并尝试在分隔符顶部覆盖一个按钮

以下是当前和预期当前情景和预期

下面是我的 XML 布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:weightSum="2">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000" />

    <RelativeLayout
        android:background="#00ff00"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="90sp"
            android:layout_height="90sp"
            app:srcCompat="@drawable/exchange"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-45dp"/>

    </RelativeLayout>

</LinearLayout>

标签: android-layout

解决方案


您可以通过以有序方式对齐布局或使用Constraint Layout.

演示.xml

[![<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light">

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_dark">

        </RelativeLayout>
    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:srcCompat="@android:drawable/btn_dialog" />
</RelativeLayout>][1]][1]

推荐阅读