操作栏菜单项未显示文本

3
我有一个只有一个项目的菜单布局。
<?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属性,但是仍然只显示了图标。

如何同时显示图标和文本?

6个回答

1

我不确定为什么 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>

1
我同意你的答案,但是我不得不做一些探索来让它在我的代码中工作,最终,我不得不为按钮创建一个onClick监听器。
在我的代码中,我在onCreateOptionsMenu(Menu menu)方法中编写了以下内容:

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

这将使menuItem在被替换为app:actionLayout中的按钮时具有响应性。

0

你可以尝试:

<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>

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

0

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

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

  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>


为了确保菜单中的文本显示,不仅要设置工具栏的app:theme,还要设置app:popuptheme。在这个例子中,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" />

尝试调整这个设置,很可能会解决你的问题。


0

我已经尝试了你的菜单XML,在Android 4.1.1和5.0.0上运行正常。

但是在4.3上无法正常工作。

为了解决这个问题,请参考如何在ActionBar图标上获取文本?

它使用自定义布局来实现该项功能。


0

从<item中删除图标

android:icon="@drawable/ic_baseline_add_24"

删除此行,则会显示,希望它对您有用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接