线性布局中位于权重分布视图之间上方的视图居中 [英] view centered above in between weight distributed views in linear layout

查看:76
本文介绍了线性布局中位于权重分布视图之间上方的视图居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下布局:

I am trying to achieve a layout as follows:

两个权重为2的视图(绿色视图)& 1(蓝色视图)呈线性布局.浮动按钮位于它们前面的这些视图之间的中心位置.但是使用线性布局是不可能的.任何人都可以在这里提供一些帮助

Two views with weight 2(green View) & 1(blue View) in a linear layout. And a floating button centered in between those views infront of them. But it is not possible using linear layout. Can anyone give a little help here

更新: 这是我所做的

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

我得到的是

推荐答案

感谢设计支持库中的"noreferrer"> CoordinatorLayout 问题可以解决.

Thanks to CoordinatorLayout in the Design Support Library this problem can be solved.

<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_anchor属性将FloatingActionButton锚定到顶视图,然后使用layout_anchorGravity

You have to anchor the FloatingActionButton to the top view using layout_anchor attribute and then set anchor gravity using layout_anchorGravity

这就是你得到的:

这篇关于线性布局中位于权重分布视图之间上方的视图居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆