Android 在线性布局中权重分布视图之间居中的视图

Android 在线性布局中权重分布视图之间居中的视图,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我试图实现如下布局: 线性布局中权重为2(绿色视图)和1(蓝色视图)的两个视图。还有一个浮动按钮,位于它们前面的视图中间。但使用线性布局是不可能的。有人能帮点忙吗 更新: 这就是我所做的 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent

我试图实现如下布局:

线性布局中权重为2(绿色视图)和1(蓝色视图)的两个视图。还有一个浮动按钮,位于它们前面的视图中间。但使用线性布局是不可能的。有人能帮点忙吗

更新: 这就是我所做的

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

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#22B14C" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="To be a floating button" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00A2E8" />
</LinearLayout>

我得到的是


如果要使用
线性布局,唯一的方法就是定义按钮的固定高度。然后,您可以像这样实现覆盖

<Button
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginTop="-24dp"
    android:layout_marginBottom="-24dp"
    android:layout_gravity="center_horizontal"
    android:text="To be a floating button"/>


但是,由于使用布局权重不是一个好的做法,我建议切换到
RelativeLayout

如果您想使用
线性布局
的唯一方法是定义按钮的固定高度。然后,您可以像这样实现覆盖

<Button
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginTop="-24dp"
    android:layout_marginBottom="-24dp"
    android:layout_gravity="center_horizontal"
    android:text="To be a floating button"/>

但是,由于使用布局权重不是一个好的做法,我建议切换到
RelativeLayout

,这要归功于在中,这个问题可以解决

<android.support.design.widget.CoordinatorLayout
    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">

        <View
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:background="#22B14C" />

        <View
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#00A2E8" />
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="#a349a4"
        app:fabSize="normal"
        app:layout_anchor="@id/top"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

您必须使用
layout\u anchor
属性将floatingAction按钮锚定到顶视图,然后使用
layout\u anchorgavity

以下是您得到的:

多亏了我们的努力,这个问题才得以解决

<android.support.design.widget.CoordinatorLayout
    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">

        <View
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:background="#22B14C" />

        <View
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#00A2E8" />
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="#a349a4"
        app:fabSize="normal"
        app:layout_anchor="@id/top"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

您必须使用
layout\u anchor
属性将floatingAction按钮锚定到顶视图,然后使用
layout\u anchorgavity

以下是您得到的:


您应该添加示例代码,以便我们能够更轻松地回答问题。您尝试过什么?您应该添加示例代码,以便我们能够更轻松地回答问题。你试过什么?这样按钮的一半似乎在下视图后面。使用相对布局当然是一个好方法。但我看不到一个简单的方法来实现它。这样按钮的一半似乎在下面的视图后面。使用相对布局当然是一个好方法。但是我看不到一个简单的方法来实现它。当我尝试这样做时,我得到了这个错误:java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换到android.support.design.widget.CoordinatorLayout$LayoutParams。你有解决方案吗?当我尝试这样做时,我得到这个错误:java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.support.design.widget.CoordinatorLayout$LayoutParams您有解决方案吗?