如何将图标设置为getbase FloatingActionsMenu [英] How to set icon to getbase FloatingActionsMenu

查看:92
本文介绍了如何将图标设置为getbase FloatingActionsMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将com.getbase.floatingactionbutton.FloatingActionsMenu用于可扩展FAB.但是无法在晶圆厂菜单上设置图标.我尝试使用可绘制的设置背景,但无法正常工作.谢谢

I'm using the com.getbase.floatingactionbutton.FloatingActionsMenu for expandable FAB. But unable to set icon on fab menu. I have tried with set background drawable, but not working. Thanks

         <FrameLayout
              android:id="@+id/frame_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/white_overlay">

         <com.getbase.floatingactionbutton.FloatingActionsMenu
             android:id="@+id/fab_menu"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="right|bottom"
             fab:fab_addButtonColorNormal="#C0007D"
             fab:fab_addButtonColorPressed="#C0007D"
             fab:fab_addButtonStrokeVisible="true"
             fab:fab_addButtonSize="normal"
             fab:fab_icon="@drawable/main_logo"
             fab:fab_labelStyle="@style/menu_labels_style"
             fab:fab_labelsPosition="left">



        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/locate_store"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="#C0007D"
            fab:fab_colorPressed="#C0007D"
            fab:fab_icon="@drawable/locate_store"
            fab:fab_size="mini"
            fab:fab_title="Locate Store" />

    </com.getbase.floatingactionbutton.FloatingActionsMenu>
</FrameLayout>

这是我的Java类代码,

Here is my Java Class code,

        final FloatingActionsMenu fabMenu = (FloatingActionsMenu)         v.findViewById(R.id.fab_menu);

        fabMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
        @Override
        public void onMenuExpanded() {
            frameLayout.getBackground().setAlpha(240);
            frameLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    fabMenu.collapse();
                    return true;
                }
            });
        }

        @Override
        public void onMenuCollapsed() {
            frameLayout.getBackground().setAlpha(0);
            frameLayout.setOnTouchListener(null);
        }
    });

推荐答案

我想出了一个可能的解决方案.它不是通用的,但可以满足我的需求.如果您觉得这不是您真正需要的,那我认为继续学习是个好主意.

I came up with a possible solution. It is not generic but it fits my needs. If you feel that is not exactly what you need I think is a good point to continue.

所以我要做的是派生库 futuresimple/android-floating-action按钮.您需要将库添加到 settings.gradle 文件中,并包含库 include':library'.然后在build.gradle文件中删除:

So what I did, is to fork the library futuresimple/android-floating-action-button. You need to add the library in the settings.gradle file and include the library include ':library'. Then in the build.gradle file I remove:

dependencies {
    compile 'com.getbase:floatingactionbutton:1.10.1'
}

我从项目中添加了库:

dependencies {
    compile project(':library')
}

然后在 FloatingActionsMenu 类中,将 private AddFloatingActionButton mAddButton; 替换为 private FloatingActionButton mAddButton; .如果您在第59行上检查 AddFloatingActionButton 类,则尝试在 AddFloatingActionButton 上尝试 setIcon 时,将引发异常.我还添加了一种更新图标colorNormal和colorPressed的方法:

Then in the FloatingActionsMenu class I replace private AddFloatingActionButton mAddButton; with private FloatingActionButton mAddButton;. If you check AddFloatingActionButton class on line number 59 it throws an exception if you try to setIcon on AddFloatingActionButton this is the reason. I also added a method to update the icon, colorNormal and colorPressed:

public void setMenuButton(int myIcon, int myColorNormal, int myColorPressed) {
        mAddButton.setIcon(myIcon);
        mAddButtonColorNormal = myColorNormal;
        mAddButton.setColorNormalResId(mAddButtonColorNormal);
        mAddButtonColorPressed = myColorPressed;
        mAddButton.setColorPressed(mAddButtonColorPressed);
    }

该方法的作用是简单地调用 FloatingActionButton 类上的所有预定义方法,并在其中更新按钮.

What the method does is simply invokes all the predefined methods on FloatingActionButton class where it updates the button.

下一步,在布局文件(.xml)中创建一个从库中获取代码的菜单按钮:

Next step create a menu button in the layout file (.xml) sample of code taken from the library:

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:id="@+id/multiple_actions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    fab:fab_addButtonColorNormal="@color/white"
    fab:fab_addButtonColorPressed="@color/white_pressed"
    fab:fab_addButtonPlusIconColor="@color/half_black"
    fab:fab_labelStyle="@style/menu_labels_style">

然后,我选择以编程方式在 *.java 文件中进行其余操作,但是也可以在xml文件中定义部分,例如定义浮动按钮.我不会详细介绍您可以检查库样本目录的地方.

Then I choosen to do the rest programmatically in *.java file but also parts can be defined in the xml file, such as define the floating buttons. I will not go through the details you can check the library sample directory.

因此,在我的 *.java 文件中,我调用了在 *.xml 文件.

So in my *.java file I call the menu button that I have created in the *.xml file.

final FloatingActionsMenu menuMultipleActions = (FloatingActionsMenu) findViewById(R.id.multiple_actions);

然后,您创建了想要添加的任意数量的按钮,例如以编程方式创建的一个按钮的示例:

Then you create as many buttons as you want to add, example for one button created programmatically sample:

final FloatingActionButton actionA = new FloatingActionButton(getBaseContext());
        actionA.setTitle("Familie");
        actionA.setIcon(R.drawable.world_map);
        actionA.setSize(FloatingActionButton.SIZE_MINI);
        actionA.setColorNormalResId(R.color.red);
        actionA.setColorPressedResId(R.color.black_semi_transparent);
        actionA.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainMapView.this, "Action Description", Toast.LENGTH_SHORT).show();
                ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).collapse();
                return;
            }
        });

此代码示例仅创建按钮,添加您选择定义的参数.根据需要添加任意数量的按钮.

This sample of code simply creates the button, add the parameters that you choose to define. Add as many buttons as you need.

然后,下一步需要设置菜单按钮.代码示例:

Then the next step that you need to do is to set the the menu button. Sample of code:

menuMultipleActions.setMenuButton(R.drawable.icon2, R.color.blue, R.color.white_pressed);

然后,最后一步是添加先前创建的按钮.代码示例:

Then last step is to add the button(s) that you previously created. Sample of code:

menuMultipleActions.addButton(actionA);

希望这可以帮助您并解决您的问题.根据我的研究,我发现其他人也使用相同的库创建了类似内容,但是现在找不到链接.如果您在网上搜索了一下,就会找到它.

Hope this helps you and solves your problem. Based on my research I have found that someone else has also created something similar with the same Library but I can not find the link now. If you search a bit online you will find it.

目前,菜单按钮上的颜色没有正确更新,但我正在处理它,如果找到解决方法,我也会在此处更新答案.希望对您有所帮助,祝您编程愉快.

For the moment the color on the menu button is not updating correctly but I am working on it, if I find a solution I will update the answer here as well. Hope this helps, happy coding.

这篇关于如何将图标设置为getbase FloatingActionsMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆