2

如何在带有图标和标题的网格中创建这种类型的子菜单。

在此处输入图像描述

4

2 回答 2

0

我已经建立了自己的菜单(附件),比如whatsapps

  1. 构建包含 framelayout 的 xml 文件,以便附件可以显示在其他布局的前面。xml 包含每个附件类型的按钮。
  2. 添加附加按钮/添加菜单按钮侦听器(在我的情况下)。
  3. 添加每次按下附加按钮以打开/关闭时都会更改的布尔值。

这是我的代码作为示例:

包含图像、名称和颜色的 XML 文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="263dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="#464646"
        android:layout_marginRight="10dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_gallery"
                android:text="Gallery"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_camera_picture"
                android:text="Photo"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_camera_video"
                android:text="Video"
                android:textColor="#ffffff" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_voice"
                android:text="Audio"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_location"
                android:text="Location"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                android:layout_weight="1"
                android:background="#303030"
                android:drawableTop="@drawable/attach_contacts"
                android:text="Contact"
                android:textColor="#ffffff" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

我的活动:

// attachment layout appear only on menu click
        attachLayout = (LinearLayout) findViewById(R.id.attachLayout);
        attachLayout.setVisibility(View.GONE);

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.attach_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (isAttachGridVisible)
            attachLayout.setVisibility(View.INVISIBLE);
        else{
            attachLayout.setVisibility(View.VISIBLE);
        }
        isAttachGridVisible = !isAttachGridVisible;

        return super.onOptionsItemSelected(item);
    }

isAttachGridVisible 是一个布尔值。

attach_menu xml 菜单文件:

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

    <item
        android:id="@+id/attachments"
        android:icon="@drawable/attachwhatsapp"
        android:orderInCategory="11111"
        android:showAsAction="always"
        android:title="">

    </item>
</menu>

祝你好运

于 2014-10-21T12:17:53.077 回答
0

您应该使用快速操作菜单,它是一个类似弹出的组件,它显示可以在对象上执行的操作。它还可用于显示自定义消息,例如锚定到屏幕特定组件的工具提示弹出窗口。

看看QuickActionMenu

快速操作教程

于 2013-08-22T06:01:27.933 回答