2013-08-22 112 views
2

如何在图标和标题的网格中创建此类子菜单。Android:选项菜单+操作栏

enter image description here

+0

您正在使用的ActionBar福尔摩斯? – Nizam

+0

是m使用操作栏与sherlock库 –

+0

子菜单选项列表iiw工作,但我想要网格图标和标题 –

回答

0

您应该使用简单操作菜单是一个弹出式组件,它能够显示一个对象上执行的操作。它也可以用来显示自定义消息,如锚定到屏幕某个组件的工具提示弹出窗口。

采取看看QuickActionMenu

QuickAction Tutorial

+0

网格是可能的吗? –

+0

我的问题是子菜单中的网格视图。 –

+0

没有必要使用gridview,你可以将它设置为水平和垂直两种方式。只需查看演示并尝试一下。 – GrIsHu

0

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

  1. 构建XML文件,它包含的FrameLayout所以附件可以显示 其他布局的前面。 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> 

好运