【问题标题】:Android Material Design Toolbar and FAB Button crossing overAndroid Material Design Toolbar 和 FAB Button 交叉
【发布时间】:2015-04-21 16:11:33
【问题描述】:

在 Google 的 Material 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 底部的问题。

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

【问题讨论】:

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


    【解决方案1】:

    在 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. 希望这可以帮助你。

    【讨论】:

    • 我没有尝试过 CoordinatorLayout,因为我最终使用了 FrameLayout
    • @pt123 这应该在你做材料设计组件时使用。
    【解决方案2】:

    使用相对布局最容易将 FAB 定位在两个视图之间。您可以使用 fab 的海拔参数将其放在工具栏上。将 FAB 的高度设置为高于工具栏的高度。

    <RelativeLayout 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"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:background="?attr/colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    
        <ListView
            android:id="@+id/listview"
            android:layout_below="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    
        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/favorite"
            android:layout_alignBottom="@+id/toolbar"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            android:elevation="8dp"
            android:layout_marginBottom="-32dp"
            fab:fab_icon="@drawable/ic_favorite_outline_white_24dp"
            fab:fab_colorNormal="@color/accent"
            fab:fab_size="mini"
            />
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-12-28
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2015-06-26
      • 2022-10-19
      • 2020-08-15
      • 2015-06-26
      • 2018-04-24
      相关资源
      最近更新 更多