android - 在折叠工具栏布局中添加 FloatingActionButton

标签 android material-design floating-action-button android-collapsingtoolbarlayout

目前正在开发一个应用程序,我需要在 折叠工具栏 的右下角添加“共享”和“添加”按钮(视差效果)。

它可能隐藏在滚动中或可能位于 Actionbar 上。添加了我想要实现的图像。目前不知道如何执行此操作(尝试使用带 anchor 的 float 操作按钮,但失败了)。

这应该是这样的:

代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/htab_maincontent"
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"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/htab_collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

        <ImageView
            android:id="@+id/htab_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleMarginTop="13dp"/>
    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/htab_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:tabIndicatorHeight="@dimen/tabIndicatorHeight"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/htab_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

最佳答案

如果您在 CollapsingToolbarLayout 中使用它,您可以这样做:

<android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbarCollapse"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="190dp"
            android:background="@android:color/darker_gray"
            android:minHeight="190dp"
            android:src="@drawable/ic_email"
            app:layout_collapseMode="parallax"
            android:id="@+id/imageView8" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabmain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@drawable/ic_add"
            app:backgroundTint="@color/ColorAccent"
            app:borderWidth="0dp"
            app:fabSize="mini"
            app:layout_anchor="@id/app_bar_layout"
            app:layout_anchorGravity="bottom|end"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_alignTop="@+id/fabmain2"
            android:layout_toStartOf="@+id/fabmain2"
            android:layout_marginEnd="20dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabmain2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginLeft="16dp"
            android:src="@drawable/ic_add"
            app:backgroundTint="@color/ColorAccent"
            app:borderWidth="0dp"
            app:fabSize="mini"
            app:layout_anchor="@id/app_bar_layout"
            app:layout_anchorGravity="bottom|end"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="23dp"
            android:layout_alignBottom="@+id/imageView8"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="29dp" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin" />

        </RelativeLayout>

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

此外,根据 Google Material Design ,我不认为这是两个按钮的标准方式/或好方式!


此外,这是我的想法(标准方式):

您可以在Actionbar 上添加一个带有该图标的Share action,然后像这样使用一个FloatingActionButton:

https://github.com/xuyisheng/DesignSupportLibraryDemo


或者我自己这样做:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!--Your content-->

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layoutdetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/thumbnaildetails"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_alignParentStart="true"
                    android:contentDescription="@string/imghd"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />

                <TextView
                    android:id="@+id/txtitemdetailstitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="85dp"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/tbbardetails"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_work"
        app:backgroundTint="#E91E63"
        app:fabSize="normal"
        app:layout_anchor="@id/app_bar_layoutdetails"
        app:layout_anchorGravity="right|bottom" />

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

关于android - 在折叠工具栏布局中添加 FloatingActionButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34805499/

相关文章:

android - 防止在 EditText 上输入键,但仍将文本显示为多行

javascript - LumX lx-select 不更新 ng-model

更改 fragment 后隐藏的Android FAB HideBottomViewOnScrollBehavior statys

java - Android Studio 3.2 更新后,我的所有 FAB 图标都很小并且位于右上角

android - ScrollView 中的按钮在编码期间未显示

java - 发现多个文件具有独立于操作系统的路径 'lib/arm64-v8a/librealm-jni.so'

android - FloatingActionButton 展开成一个新的 Activity

angular - 如何为 Angular Material 表提供固定高度

java - 如何在垂直 ScrollView 上使用 float 按钮?

java - 如何修复错误 : JAVA_HOME is set to an invalid directory (Flutter android licenses)