如何将浮动操作菜单附加到现有的浮动操作菜单?

时间:2017-09-12 10:00:58

标签: android menu floating-action-button

我试图让我的一个浮动动作按钮有多种选择。

我的屏幕侧面有一个浮动操作按钮。

当我按下它时,弹出三个按钮,第一个是搜索按钮,第二个是添加按钮,第三个是设置按钮。 image 1

当我按下添加按钮时,我希望它出现多个按钮。 image 2

我已经查看了 SO 以及Android Arsenal和YouTube等其他网站的教程,但还没找到任何内容。

我已经开始编写代码,但即使它没有错误构建也无法正常工作。

如果有帮助

,我正在使用com.oguzdev:CircularFloatingActionMenu:1.0.2作为FAB库

Image 1

Image 2

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private String[] navDrawerItemTitles;
    private DrawerLayout drawerLayout;
    private ListView listView;
    Toolbar toolbar;
    public static final String TAG_ADD = "Add";
    private CharSequence drawerTitle;
    private CharSequence title;
    android.support.v7.app.ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.Green));
        title = drawerTitle = getTitle();
        navDrawerItemTitles = getResources().getStringArray(R.array.nav_d_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawer);
        listView = (ListView) findViewById(R.id.left_drawer);

        setupToolbar();

        // ToolBox Image
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.hammers_green);
        // Settings
        ImageView SettingsImagView = new ImageView(this);
        SettingsImagView.setImageResource(R.drawable.settings_green);
        // Add Button
        ImageView AddButton = new ImageView(this);
        AddButton.setImageResource(R.drawable.plus_green);
        // Search Button
        ImageView SearchImage = new ImageView(this);
        SearchImage.setImageResource(R.drawable.searches);
        // Tag Green
        ImageView TagGreen = new ImageView(this);
        TagGreen.setImageResource(R.drawable.tag_green_circle);
        // Tag Grey
//        ImageView TagGrey = new ImageView(this);
//        TagGrey.setImageResource(R.drawable.);
        // Tag Blue
        ImageView TagBlue = new ImageView(this);
        TagBlue.setImageResource(R.drawable.tag_blue_circle);
        // Tag Red
//        ImageView TagRed = new ImageView(this);
//        TagRed.setImageResource(R.drawable.);

        // Layout Parameters for the Toolbox Button
        FloatingActionButton.LayoutParams layoutParams = new FloatingActionButton.LayoutParams(300, 300);


        FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(imageView)
                .setLayoutParams(layoutParams)
                .setPosition(3)
                .build();

        // SubActionBuilder for Initial Toolbox Button Click
        SubActionButton.Builder ToolboxButtons = new SubActionButton.Builder(this);
        SubActionButton Settings = ToolboxButtons.setContentView(SettingsImagView).build();
        SubActionButton Add = ToolboxButtons.setContentView(AddButton).build();
        SubActionButton Search = ToolboxButtons.setContentView(SearchImage).build();

        // SubActionBuilder for Add Button Click
        SubActionButton.Builder AddButtonSelections = new SubActionButton.Builder(this);
        SubActionButton TagGreenButton = AddButtonSelections.setContentView(TagGreen).build();
        SubActionButton TagBlueButton = AddButtonSelections.setContentView(TagBlue).build();
        Add.setOnClickListener(this);
        Add.setTag(TAG_ADD);


        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(Settings, 200, 200)
                .addSubActionView(Add, 400, 400)
                .addSubActionView(Search, 200, 200)
                .setRadius(400)
                .attachTo(actionButton)
                .build();



        FloatingActionMenu actionMenu1 = new FloatingActionMenu.Builder(this)
                .attachTo(Add)
                .addSubActionView(TagGreenButton, 150, 150)
                .addSubActionView(TagBlueButton, 150, 150)
                .setRadius(200)
                .build();





        DrawerObjectDefiner[] drawerObjectDefiners = new DrawerObjectDefiner[5];

        drawerObjectDefiners[0] = new DrawerObjectDefiner(R.drawable.house, "Home");
        drawerObjectDefiners[1] = new DrawerObjectDefiner(R.drawable.import_export_green, "Import/Export");
        drawerObjectDefiners[2] = new DrawerObjectDefiner(R.drawable.photo_green, "Pictures");
        drawerObjectDefiners[3] = new DrawerObjectDefiner(R.drawable.settings_icon, "Settings");
        drawerObjectDefiners[4] = new DrawerObjectDefiner(R.drawable.qustion_mark_green, "Help");

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(true);

        DrawerCustomAdapter adapter = new DrawerCustomAdapter(this, R.layout.list_view_item_row, drawerObjectDefiners);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new DrawerItemClickListener());
        drawerLayout = (DrawerLayout)findViewById(R.id.mainDrawer);
        drawerLayout.addDrawerListener(drawerToggle);
        setupDrawerToggle();


    }

    @Override
    public void onClick(View view) {
        if (view.getTag().equals(TAG_ADD)){
            actionMenu();
        }


    }

activtiy_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android= "http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainDrawer"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/Green">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/container_toolbar"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_height="0dp"
                android:layout_width="0dp" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="38dp"
                android:layout_height="31dp"
                app:srcCompat="@drawable/menu_green"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="@+id/toolbar"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/toolbar"
                android:layout_marginBottom="8dp"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintVertical_bias="0.555"
                android:contentDescription="@string/Menu"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp" />

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <ListView
                android:layout_width="400dp"
                android:layout_height="596dp"
                android:layout_marginRight="8dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginLeft="8dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="8dp"
                app:layout_constraintHorizontal_bias="0.562"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    app:layout_constraintVertical_bias="0.489" />

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/White"
        android:choiceMode="singleChoice"
        android:divider="@color/Green"
        android:dividerHeight="1dp">

    </ListView>
</android.support.v4.widget.DrawerLayout>

如果您需要任何其他信息,请随时提出。

1 个答案:

答案 0 :(得分:0)

所以我想通了。

我需要做的就是将按钮更改为公共变量,并将处理添加按钮的Floating Action Menu方法放入添加按钮onclicklistener

像这样:

@Override
public void onClick(View view) {
    if (view.getTag().equals(TAG_ADD)){
        FloatingActionMenu actionMenu1 = new FloatingActionMenu.Builder(this)
                .attachTo(Add)
                .addSubActionView(TagGreenButton, 150, 150)
                .addSubActionView(TagBlueButton, 150, 150)
                .setRadius(200)
                .build();
    }


}

感谢每个人的帮助!!