Android材质设计工具栏和FAB按钮交叉

时间:2015-04-21 16:11:33

标签: android-appcompat material-design android-toolbar floating-action-button

在Google的Material Design规范中,他们经常会显示浮动操作按钮位于工具栏的一半以上并覆盖内容。 image of the design

http://www.google.com/design/spec/components/buttons-floating-action-button.html

但是我尝试了一些变化,工具栏和内容之间仍然存在差距,这是由按钮引起的。

<LinearLayout>
 <include layout="@layout/toolbar" />   

  <include layout="@layout/fab_button" />

  <ScrollView>
    Content
  </ScrollView>
</LinearLayout>

我也尝试将工具栏和FAB按钮放在FrameLayout中,它也有差距。 FAB按钮代码取自Google的样本,我没有遇到过在RecyclerView底部重叠的问题。

有没有办法实现材料设计规范中显示的外观。

2 个答案:

答案 0 :(得分:2)

在FAB中添加layout_anchor属性并将其设置为“顶视图”。

CoordinatorLayout作为根视图,这将是最佳布局实践。

您可以使用FAB中的layout_anchorGravity属性设置FAB重力。

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

        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:background="@android:color/holo_blue_bright"
            android:orientation="horizontal"/>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@android:color/holo_green_light"
            android:orientation="horizontal"/>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_done"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|right|end"/>
    </android.support.design.widget.CoordinatorLayout>

Check this out. 希望这可以帮助你。

答案 1 :(得分:1)

使用相对布局最容易在两个视图之间放置FAB。您可以使用fab的高程参数来通过工具栏获取它。将FAB的高程设置为工具栏的高程。

class Example {
    int a;
    int[][] f;
    JLabel[][] label = new JLabel[3][];
    String s;
}

class Demo {

    public static void main(String[] ar) {
        Class<?> cls = Example.class;
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            System.out.println(f.getType().getTypeName());
        }

    }
}
相关问题