将按钮放在两个不同的 LinearLayouts 上

Placing button over two different LinearLayouts

所以,我想在中间底部屏幕上添加一个带有文本 "Already Registered?" 的按钮,但我的代码包含两个不同的 LinearLayouts,用于左半边和右半边。我希望按钮一半在左侧 Linearlayout 上,一半在右侧。此外,它们是可点击的,所以就我而言,我必须将它们放在我当前的布局中,而不是包括它们。

我目前拥有的:

我想得到的:

我的代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    tools:context=".MainActivity"
    android:weightSum="2"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/customerLinearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_weight="1"
        android:background="@color/lightblueMainActivity"
        android:onClick="customerSignUp">

        <TextView
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="@color/orangeMainActivity"
            android:text="@string/customerMainActivity"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/electricianLinearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_weight="1"
        android:background="@color/orangeMainActivity"
        android:onClick="electricianSignUp">

        <TextView
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textSize="24sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightblueMainActivity"
            android:text="@string/electricianMainActivity"/>
    </LinearLayout>
</LinearLayout>

提前致谢!

除了LinearLayout之外,还有其他类型的ViewGroup可以达到"split screen"的效果,但是为了简单起见,我们使用加权LinearLayout来划分屏幕。

但是子 Views 可以是 TextViews(不需要中间 ViewGroup),因为你可以让它们有背景颜色并控制文本对齐方式。

因为你想让Button重叠在屏幕的两部分,你可以把它和LinearLayout放到一个FrameLayout中(我用了一个TextView但是属性基本相同):

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Customer"
            android:textColor="#aaaaaa"
            android:textSize="20sp"
            android:gravity="center"
            android:background="#0000ff"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="Electrician"
            android:textColor="#666666"
            android:textSize="20sp"
            android:gravity="center"
            android:background="#ffab00"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="24dp"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:text="Already Registered?"/>
</FrameLayout>