Android layout Android-顶部带有按钮的拆分视图

Android layout Android-顶部带有按钮的拆分视图,android-layout,Android Layout,我试图在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:or

我试图在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>

可以通过按顺序对齐布局或使用约束布局来实现这一点

demo.xml

可能最简单的方法就是使用FrameLayout来包装现有布局:

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

    <!-- your current LinearLayout goes here, except for the ImageButton -->

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="90sp"
        android:layout_height="90sp"
        android:layout_gravity="center"
        app:srcCompat="@drawable/exchange"/>

</FrameLayout>

通过这种方式,您可以将所有内容展平为一个视图组。

在框架布局代码中,两个内部视图不会显示,我看到的只是白色背景。而在约束一中,顶部的红色占据了整个屏幕。
<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:id="@+id/topView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">



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

<LinearLayout
    android:id="@+id/bottomView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff0000"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <RelativeLayout
        android:id="@+id/bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#00ff00"
        app:layout_constraintTop_toBottomOf="@+id/top"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="90sp"
        android:layout_height="90sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/exchange"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<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:id="@+id/topView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">



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

<LinearLayout
    android:id="@+id/bottomView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</RelativeLayout>