ActionBar menuItem 不显示文本

ActionBar menuItem not showing text

我的菜单布局中只有一项。

<?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/toolbar_right_button"
        android:icon="@drawable/ic_toolbar_locked"
        android:orderInCategory="1"
        android:title="Logout"
        app:showAsAction="always|withText"/>
</menu>

如您所见,我已经包含了 withText 属性,但我仍然只得到图标。

如何让图标和文字同时显示?

我试过你的菜单 xml,它适用于 android 4.1.1、5.0.0

但它不适用于 4.3

要解决这个问题,请参考How to get text on an ActionBar Icon?

它使用项目的自定义布局

我不确定为什么 withText 对我不起作用,所以我创建了一个 actionLayout 来代替。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/toolbar_right_button"
        android:icon="@drawable/ic_toolbar_locked"
        android:orderInCategory="1"
        android:title="Logout"
        app:actionLayout="@layout/menu_item_logout"
        app:showAsAction="ifRoom|withText"/>
</menu>

actionLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/logout_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_toolbar_locked"
        android:gravity="center_vertical"
        android:text="Logout"
        android:textColor="@color/PrimaryColour"/>
</RelativeLayout>

我同意你的回答,但我必须做一些挖掘才能让它在我的代码中工作,最终,我必须为按钮创建一个 onClickListener。

在我的代码中,我在onCreateOptionsMenu(Menu menu)方法中这样写:

menu.getItem(indexInMenu).getActionView().findViewById(R.id.button_logout).setOnClickListener(...);

这使得 menuItem 在被替换为 app:actionLayout

中的按钮时响应

你可以试试:

<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" >
    <!-- "Mark Favorite", should appear as action button if possible -->
    <!-- Settings, should always be in the overflow -->
    <item
        android:title="Settings"
        android:id="@+id/mainMenu"
        android:icon="@drawable/ic_3d_rotation_black_24dp"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/menu_logout1"
                android:icon="@drawable/ic_3d_rotation_black_24dp"
                android:title="logout1"/>
            <item
                android:id="@+id/menu_logout3"
                android:icon="@drawable/ic_3d_rotation_black_24dp"
                android:title="logout3"/>
        </menu>
    </item>
    <item
        android:id="@+id/menu_logout2"
        android:icon="@drawable/ic_3d_rotation_black_24dp"
        android:title="logout2"
        app:showAsAction="always"/>
</menu>

请注意,此示例还将绘制一些图标,因此如果您不想要这些图标,只需删除 icon 属性即可。

中删除图标

android:icon="@drawable/ic_baseline_add_24"

删除这一行然后它会显示希望它对你有用

当您的工具栏菜单项中的文本不显示时,这可能与您的样式设置有关。例如,当您以工具栏中的应用程序标题以白色显示的方式组织样式时,您的菜单项可能会变成白色。

要解决这个问题,您需要为工具栏设置两个属性:

  1. 这是工具栏的通用样式。文本颜色为白色。也许你在面对“无文本”问题时正在使用这样的东西
<style name="ToolBarStyle" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
</style>

  1. 要确保显示菜单中的文本,不仅要设置工具栏的 app:theme,还要设置 app:popuptheme。 在这个例子中弹出主题设置为浅色主题,因为这会导致黑色文本。
<androidx.appcompat.widget.Toolbar
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@color/supplyon_blue"
            app:theme="@style/ToolBarStyle"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
            android:id="@+id/toolbar" />

试试这个设置,它可能会解决您的问题。