Android如何在单击按钮后打开选项菜单项?

时间:2019-05-02 10:29:10

标签: android menuitem android-menu android-optionsmenu

我正在使用这样的菜单项。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_overflow"
        android:enabled="true"
        android:icon="@drawable/ic_menu_moreoverflow_mtrl_alpha"
        android:title=""
        app:showAsAction="ifRoom">
        <menu>
            <item
                android:id="@+id/action_city"
                android:icon="@drawable/ic_change_city"
                android:title="Change City"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_language"
                android:icon="@drawable/ic_change_language"
                android:title="Change Language"
                app:showAsAction="never" />
            <item
                android:id="@+id/action_change_theme"
                android:icon="@drawable/ic_change_theme"
                android:title="Change Theme"
                app:showAsAction="never" />
        </menu>
    </item>

</menu>

现在如何在单击按钮后以编程方式展开菜单项-> menu_overflow

我的代码是:

            activity.openOptionsMenu();
            ---------
            toolbar.showOverflowMenu();
            ---------
            menu_overflow.expandActionView();
            ---------
            menu_overflow.getSubMenu().getItem().expandActionView();

但是上面的那些代码不起作用。

这里是设计外观。 enter image description here

2 个答案:

答案 0 :(得分:0)

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity stuff is

    generic(
        CW : integer := 3
    );
    port(    
        sel1 : in    std_logic_vector(CW-1 downto 0);
        val1 : out   std_logic
    );

end entity;

architecture rtl of stuff is

    function n(n_value:integer; n_width:integer) 
        return std_logic_vector is
    begin
        return std_logic_vector(to_unsigned(n_value, n_width));
    end function;

    constant CMD1 : std_logic_vector(2 downto 0) := n(0, 3);
    constant CMD2 : std_logic_vector(2 downto 0) := n(1, 3);
    constant CMD3 : std_logic_vector(2 downto 0) := n(2, 3);
    constant CMD4 : std_logic_vector(2 downto 0) := n(3, 3);
    constant CMD5 : std_logic_vector(2 downto 0) := n(4, 3);

    signal sel2 : std_logic_vector(2 downto 0);

begin

    sel2 <= sel1(2 downto 0);

    process(sel2)
    begin
        case sel2 is
            when CMD1   => val1 <= '0';     
            when CMD2   => val1 <= '1';     
            when CMD3   => val1 <= '0';     
            when others => val1 <= '0';     
        end case;
    end process;

end architecture;

答案 1 :(得分:0)

尝试一下

YourActivity.this.openOptionsMenu();