【问题标题】:ActionBar menuItem not showing textActionBar menuItem 不显示文本
【发布时间】:2015-03-19 22:52:33
【问题描述】:

我有一个菜单布局,其中只有一项。

<?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 属性,但我仍然只得到图标。

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

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

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

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

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

    当 menuItem 被 app:actionLayout 中的按钮替换时,它会做出响应

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        但它不适用于 4.3

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

        它为项目使用自定义布局

        【讨论】:

          【解决方案4】:

          你可以试试:

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

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

          【讨论】:

            【解决方案5】:

            android:icon="@drawable/ic_baseline_add_24"
            

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

            【讨论】:

              【解决方案6】:

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

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

              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。 在此示例中,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" />
              

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-10-10
                • 2017-05-31
                • 2013-06-07
                • 2010-11-02
                • 1970-01-01
                • 2017-06-04
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多