6

我正在使用库https://github.com/futuresimple/android-floating-action-button创建运行良好的 fab 按钮。我尝试了几个库,但没有一个使用在大多数应用程序中使用的效果与 fab(或 Google 应用程序,如 keep、inbox),当单击 fab 时,整个应用程序的背景变成半透明的白色或黑色,这取决于。我找不到任何方法来做到这一点。是否有可能有一个例子或类似的东西来做这种效果?你可以在这张图片中看到我的意思: 在此处输入图像描述

半黑背景

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/semi_white_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_semi_transparent"
        android:visibility="gone" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/listFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/content_frame"
        android:visibility="gone" >

        <TextView
            android:id="@+id/status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cabinet_color"
            android:fontFamily="sans-serif"
            android:gravity="center_vertical"
            android:paddingBottom="8dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="8dp"
            android:textColor="#fff"
            android:textSize="17sp"
            android:visibility="gone" />

        <android.support.v7.widget.RecyclerView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/status"
            android:clipToPadding="false"
            android:scrollbars="vertical" />

        <LinearLayout
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/emptyImage"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="16dp"
                android:scaleType="fitXY"
                android:src="?empty_image" />

            <TextView
                android:id="@+id/emptyText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center"
                android:lineSpacingMultiplier="1.4"
                android:paddingBottom="16dp"
                android:text="@string/no_files"
                android:textColor="?empty_text"
                android:textSize="22sp" />
        </LinearLayout>
    </RelativeLayout>

    <ProgressBar
        android:id="@android:id/progress"
        style="?android:progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminateOnly="true" />

</RelativeLayout>

这是我的布局。

4

4 回答 4

5

我发现这是我认为最简单的解决方案,并且效果很好:

fam.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
        @Override
        public void onMenuExpanded() {
            dimmedBackground.setVisibility(View.VISIBLE);
        }

        @Override
        public void onMenuCollapsed() {
            dimmedBackground.setVisibility(View.GONE);
        }
    });
于 2015-04-13T13:02:03.243 回答
2

喜欢你的问题...

该库不允许您设置添加按钮单击事件,您可能必须为自己更改库代码,使用这个想法:使用 Relatie 或 framelayout 并在 FAB 后面添加黑色背景或图像。

现在在库类 FloatingActionsMenu.class 中找到并注释此代码:

//mAddButton.setOnClickListener(new OnClickListener()
//{
//  @Override
//  public void onClick(View v)
//  {
//      toggle();
//  }
//});

然后在那个类中添加这个方法,

public void setAddButtonClickListener(OnClickListener listener)
{
mAddButton.setOnClickListener(listener);
}

在您的活动中定义点击侦听器,例如

OnClickListener listener = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        if (floatingActionMenuButton.isExpanded())
        {//make the background visible
        }
        else
        {//make the background invisible
        }
        floatingActionMenuButton.toggle();
    }
};

并将其传递给 FAB

floatingActionMenuButton.setAddButtonClickListener(listener);

构建库和项目,希望这会有所帮助!

于 2015-03-30T13:03:04.743 回答
1

您需要将活动包装在 FrameLayout 中。FrameLayout 应该有 2 个孩子

  • 您的活动内容
  • 另一个包含 FloatingActionsMenu 的 Framelayout

这是相关的代码片段。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.city.bytes.view.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- other content in the activity -->
</LinearLayout>

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_overlay">
    <!--  floating action menu with buttons -->
</FrameLayout>

frame_layout在您的活动中,更改单击 FAB 菜单时的 alpha 。我还写了一篇博客来解释它。http://www.risabhsinghal.in/implement-floating-action-button-similar-to-inbox-by-gmail-or-evernote/

于 2015-08-22T11:58:08.440 回答
0
floatingActionMenu.setOnMenuToggleListener(
  new FloatingActionMenu.OnMenuToggleListener() { 
     @Override 
     public void onMenuToggle(boolean opened) {
         if(opened){
           //show backgroud
         }else{
           //hide backgroud
         }
     }
  });
于 2018-10-19T12:18:41.137 回答