【问题标题】:Show overflow menu on Image button click单击图像按钮时显示溢出菜单
【发布时间】:2015-10-23 15:38:06
【问题描述】:

我想在点击ImageButton 时显示溢出菜单。

菜单/menu_scrollable_tab.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.example.scrollingtab.activity.ScrollableTabsActivity">

    <item android:id="@+id/action_settings" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="always"
        android:icon="@drawable/info" />

      <item android:id="@+id/action_settings1" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />

        <item android:id="@+id/action_settings2" 
        android:title="@string/action_settings"
        android:orderInCategory="100" 
        app:showAsAction="never" />
</menu>

我已禁用默认操作栏并使用布局创建操作栏。我没有使用默认操作栏或工具栏,因为我需要更多地自定义操作栏。

布局/actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="50dp" >
 <LinearLayout      
     android:layout_width="fill_parent"
     android:layout_height="50dp"
     android:orientation="horizontal"
     android:background="@color/actionbar_bg"
     android:weightSum="2">

     <ImageButton android:id="@+id/ib_navigation"
         android:background="@drawable/nv_drawer_24x24"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>

     <View 
         android:layout_width="0dp"
         android:layout_height="50dp"
         android:layout_weight="1.1"/>

     <ImageButton android:id="@+id/ib_navigation1"
         android:background="@drawable/info"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>

     <ImageButton android:id="@+id/ib_navigation2"
         android:background="@drawable/ic_drawer"
         android:layout_width="0dp"
         android:layout_weight=".3"
         android:layout_height="50dp"/>     
 </LinearLayout>
</RelativeLayout>

根据客户要求,我使用布局设计了操作栏,外观和感觉都很好。

默认的OnCreateOptionsMenu(Menu menu) 方法不会填充溢出菜单。现在我想在点击ImageButtonid= ib_navigation2 时填充溢出菜单。我没有任何想法来实现这一点。

谁能帮我做这件事。

谢谢

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    你需要重写创建你自己的菜单的方法

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        return true;
    }
    

    buttonclick可以调用openOptionsMenu();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openOptionsMenu();
        }
    });
    

    否则

    public myOnClickMethod(View v) {
        openOptionsMenu();
    }
    

    有关菜单的更多信息,请参见此处 http://developer.android.com/guide/topics/ui/menus.html

    希望这对你有用

    【讨论】:

    • 我在 onCreate 方法中添加了按钮 onclick 的代码。这是有效的,但菜单显示在移动屏幕的底部。是否可以在我单击的按钮下方显示溢出菜单。
    • 嗨任何想法如何使它在我要点击的按钮下方。
    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多