Android FloatingActionButton: FloatingActionsMenu向下伸展弹出及删除包含的FloatingActionButton【4】



Android第三方开源FloatingActionButton(com.getbase.floatingactionbutton): FloatingActionsMenu向下伸展弹出及删除包含的FloatingActionButton【4】

FloatingActionsMenu里面嵌套的普通FloatingActionButton可以在上层Java代码动态删除。同时,FloatingActionsMenu不仅可以向上弹出伸展,也可以向下弹出伸展。
写一个例子说明。
先写一个布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="zhangphil.fab.MainActivity" >

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/multiple_actions_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        fab:fab_addButtonColorNormal="@color/white"
        fab:fab_addButtonColorPressed="@color/white_pressed"
        fab:fab_addButtonPlusIconColor="@color/half_black"
        fab:fab_addButtonSize="normal"
        fab:fab_expandDirection="down"
        fab:fab_labelStyle="@style/menu_labels_style" >

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_size="mini"
            fab:fab_title="FAB 1" />

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/blue_semi_transparent"
            fab:fab_colorPressed="@color/blue_semi_transparent_pressed"
            fab:fab_title="点击删除" />

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/white_pressed"
            fab:fab_title="FAB 2" />
    </com.getbase.floatingactionbutton.FloatingActionsMenu>

</RelativeLayout>

其中,

fab:fab_expandDirection="down"
控制FloatingActionsMenu里面包含的FloatingActionButton弹出伸展方向。


Java代码:

package zhangphil.fab;

import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//点击删除蓝色的那个按钮
	    final FloatingActionButton removeAction = (FloatingActionButton) findViewById(R.id.button_remove);
	    removeAction.setOnClickListener(new OnClickListener() {
	      @Override
	      public void onClick(View v) {
	        ((FloatingActionsMenu) findViewById(R.id.multiple_actions_down)).removeButton(removeAction);
	      }
	    });
	}
}

代码运行结果如图所示:



(注意:本文中在xml布局文件中涉及到的个别属性如colors,styles等等值,请参考附录文章3)


附录参考文章:
【文章1】《Android第三方开源FloatingActionButton(com.getbase.floatingactionbutton): FloatingActionsMenu【3】》链接地址:http://blog.csdn.net/zhangphil/article/details/50167609
【文章2】《Android第三方开源FloatingActionButton(com.getbase.floatingactionbutton)【2】》链接地址:http://blog.csdn.net/zhangphil/article/details/50166929
【文章3】《Android第三方开源FloatingActionButton(com.getbase.floatingactionbutton)【1】》链接地址:http://blog.csdn.net/zhangphil/article/details/50166715

【文章4】《Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout》链接地址:http://blog.csdn.net/zhangphil/article/details/48861371
【文章5】《Android第三方FloatingActionButton:伴随ListView、RecyclerView、ScrollView滚动滑入滑出》链接地址:http://blog.csdn.net/zhangphil/article/details/50135707 

你可能感兴趣的:(android)