【问题标题】:Alpha background on FAB clickedFAB 上的 Alpha 背景点击
【发布时间】:2015-06-03 06:35:00
【问题描述】:

我正在使用库 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>

这是我的 layout.xml,但似乎根本无法按我的意愿工作。似乎没有与整个视图重叠,而只是为它想要的颜色..

【问题讨论】:

  • 没有得到你,只是简单的描述问题。

标签: java android android-background floating-action-button


【解决方案1】:
floatingActionMenu.setOnMenuToggleListener(
  new FloatingActionMenu.OnMenuToggleListener() { 
     @Override 
     public void onMenuToggle(boolean opened) {
         if(opened){
           //show backgroud
         }else{
           //hide backgroud
         }
     }
  });

【讨论】:

    【解决方案2】:

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

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

    这里是相关代码sn-p。

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 太棒了,我使用了来自这个库 github.com/Clans/FloatingActionButton 的代码,对于同样的工作,该代码适用于这个库:floatingActionMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() { @Override public void onMenuToggle(boolean opens ) { displayToast("opened is "+opened); } });
      【解决方案4】:

      喜欢你的问题...

      这个库不允许你设置添加按钮点击事件,你可能需要为自己更改库代码,使用这个想法: 使用 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);
      

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

      【讨论】:

      • 你的想法似乎更正了.. 只是一件事.. 正如你所看到的,FloatingActionMenuButton 是“主要”工厂,当点击打开其他 FloatingActionButtons.. 如果我评论那几行.. 它仍然有效?我的意思是,它会打开其他浮动按钮还是只创建背景?我希望我没有在它们之间做出决定.. 我会同时拥有这两种功能
      • floatingActionMenuButton.toggle();在点击监听器会打开或关闭其他菜单按钮,你也可以同时改变背景。
      • 这是一个惊人的消息。完美,我一回家我就试试你的解决方案。同时,谢谢你
      • 嗯,它有效..但不完全符合我的要求;我可以使我创建的 RelativeLayou 变得可见和不可见,但是如果我使用半白色,它只会为工具栏着色。或者如果我使用半黑色,例如,颜色似乎不会与我在布局中的回收视图重叠..我不知道该怎么形容
      • 我将用布局更新问题,以便您看到
      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 2018-05-07
      • 2013-04-28
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      相关资源
      最近更新 更多