40

我在 xml 文件中尝试了不同的组合:

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="ifRoom|withText" />
</menu>

或者

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="always|withText" />
</menu>

或者

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="withText" />
</menu>

或者

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText" />
</menu>

我试图以编程方式设置它

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        MenuItem item = menu.add(R.string.menu_create_alarm);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT|MenuItem.SHOW_AS_ACTION_IF_ROOM);
        item.setIcon(R.drawable.ic_action_accept);
        item.setOnMenuItemClickListener(
            new OnMenuItemClickListener(){

                @Override
                public boolean onMenuItemClick(MenuItem item){
                    saveAlarm();
                    return true;
                }
            }
        );


//      inflater.inflate(R.menu.menu_create_alarm, menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

或者

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText"
        app:showAsAction="always|withText" />
</menu>

但是,仅显示图标。并且有空间,因为我没有设置工具栏标题。删除菜单并用工具栏中的按钮替换它们是不合适的。
如何显示文字?

4

9 回答 9

27
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_patient_home_screen, menu);


        menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
        menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
        menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
        menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 2:
                Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 3:
                Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 4:
                Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private CharSequence menuIconWithText(Drawable r, String title) {

        r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
        SpannableString sb = new SpannableString("    " + title);
        ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return sb;
    }

模拟器截图

希望对你有帮助

于 2017-01-10T13:04:44.977 回答
23

您需要添加tools:context="your class"到菜单标签

菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
   tools:context=".activities.BaseActivity">


   <item
       android:id="@+id/action_notification1"
       android:icon="@drawable/three"
       android:title="action_notification"
       app:showAsAction="always">
       <menu>
           <item
               android:id="@+id/profile"
               android:icon="@drawable/profile"
               android:orderInCategory="100"
               android:title="PROFILE" />

           <item
               android:id="@+id/c"
               android:icon="@drawable/correct_tick"
               android:orderInCategory="100"
               android:title="COMPLETED TRIPS" />

           <item
               android:id="@+id/app"
               android:icon="@drawable/report_issue"
               android:orderInCategory="100"
               android:title="REPORT ISSUES" />
           <item
               android:id="@+id/r"
               android:icon="@drawable/correct_tick"
               android:orderInCategory="100"
               android:title="REACHED CENTER" />


           <item
               android:id="@+id/pdf"
               android:icon="@drawable/pdf_image"
               android:orderInCategory="100"
               android:title="BAG INFO" />
           <item
               android:id="@+id/l"
               android:icon="@drawable/logout"
               android:orderInCategory="100"
               android:title="LOGOUT" />
       </menu>
   </item>


</menu>

覆盖onCreateOptionsMenu()方法

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu); 
    menu.getItem(0).getSubMenu().getItem(3).setVisible(false);
    menu.getItem(0).getSubMenu().getItem(4).setVisible(true);
    return super.onCreateOptionsMenu(menu);
} 

您应该写入tool:context菜单标签然后运行您将在文本之前获得图标。

于 2016-03-23T10:28:14.370 回答
21

除了来自 dev_ry的答案之外,还有一种更流畅的方法,无需使用反射,只需强制转换和抑制受限 API 警告:

import android.support.v7.view.menu.MenuBuilder;

@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (menu instanceof MenuBuilder) {
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
    }
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}
于 2017-04-14T12:15:20.793 回答
12
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
    if (menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(),
                        "onMenuOpened...unable to set icons for overflow menu",
                        e);
            }
        }
    }
    return super.onPrepareOptionsPanel(view, menu);
}
于 2016-02-19T07:02:09.507 回答
10

xml:

<menu 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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText"
        app:showAsAction="always|withText" />
</menu>

爪哇:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.menu_frg_safetybox, menu);
    super.onCreateOptionsMenu(menu, inflater);
}
于 2015-10-06T12:24:46.307 回答
8

您可以使用以下代码来确定您的目的。无需使其程序化。您可以在 menu.xml 文件中进行欺骗。

菜单.xml

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

<item
    android:id="@+id/overflowMenu"
    android:icon="@drawable/ic_more_vert_white_24dp"
    android:title=""
    app:showAsAction="always">

    <menu>
        <item
            android:id="@+id/menuWithIconText"
            android:icon="@drawable/chosen_icon"
            android:title="@string/menu_item_title"
            />
    </menu>
</item>

在上面的代码中,我们使用menu了 inside of item。在内部菜单中,我们用图标定义了我们想要的溢出菜单。请记住app:showAsAction="always"在外部item标签中使用。

于 2020-03-01T12:15:07.693 回答
6

如果有足够的空间, 'always|withText'将起作用,否则它将仅放置图标。您可以通过将手机旋转到横向模式来查看.​​.

       <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_launcher"
        android:title="Save"
        app:showAsAction="always|withText"
        />
于 2015-10-06T12:42:41.210 回答
3

试试这两个

app:showAsAction="always|withText"
android:showAsAction="always|withText"
于 2015-10-06T12:20:55.627 回答
2

我的活动.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        View view = menu.findItem(R.id.whats_this).getActionView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MyActivity">

    <item
        android:id="@+id/item"
        app:actionLayout="@layout/my_layout"
        android:orderInCategory="100"
        android:title="@string/whats_this"
        app:showAsAction="always" />
</menu>

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:paddingLeft="5dp"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:text="@string/whats_this"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@mipmap/question"/>

</LinearLayout>
于 2020-01-20T13:13:28.283 回答