只有文本的菜单未显示在工具栏中

3

我需要在工具栏中展示返回图标、Logo和文本(Next)。Next需要右对齐。我尝试通过菜单放置文本。我已经将Logo和返回按钮设置到了工具栏中。

toolbar.setLogo(R.mipmap.logo);
toolbar.setNavigationIcon(R.mipmap.back);

另外,在onCreateOptionsMenu中,我已经膨胀了以下菜单

膨胀的菜单:

<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"
>
<item
    android:id="@+id/action_next"
    android:title="@string/next"
    app:showAsAction="always" />

文本菜单完全不可见。如果我在菜单项中放置图标,它就会显示出来。 我使用了android.support.v7.widget.Toolbar。

编辑:

问题已解决。如果有人遇到类似的问题,请添加以下内容。

请添加

<item name="android:actionMenuTextColor">@color/blue</item>

在样式中,点击菜单项会被选中但不可见,意味着它在白色工具栏上显示白色文本颜色。设置menuText颜色解决了我的问题。


我认为你的工具栏标志和导航图标占用了很多空间... 你是否添加了 setSupportActionBar(toolbar) - Abhishek Singh
如果您正在使用白色工具栏,为什么要使用深色主题... 如果您使用浅色主题,则菜单文本会变成黑色,这是最好的解决方案。 - Abhishek Singh
我使用了浅色主题。但是在某些地方,我已经指定colorPrimary为白色。 - seema
3
@seema,我认为你应该将你的解决方案发布为答案并接受它。我也遇到了同样的问题。 - Vadim Kotov
5个回答

2

Android文档解释了showAsAction选项中的withText将会:

同时将动作项的标题文本(定义在android:title中)包括在内。

既然你需要这个,那么请编写以下代码。

<item
android:id="@+id/action_next"
android:title="@string/next"
app:showAsAction="always|withText" />

1
谢谢,通过在样式中添加菜单文本颜色来解决了。现在它可以工作了。 - seema

0
如果您在操作中得到了一个没有文本但有空格的空动作,那么可能是因为您在布局中创建了自己的工具栏,并且还使用了主题父级中的操作栏。如果您正在创建自己的工具栏,则应在您的主题中使用parent="Theme.MaterialComponents.DayNight.NoActionBar",包括正常和夜间主题。

0

这是由于您布局中使用的样式引起的。

为了解决这个问题,请定义一个新的样式并在您的布局中使用它。

<style name="ToolBarStyle" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
    <item name="android:itemBackground">#fff</item>  // background color of the menus
    <item name="android:textColor">#000</item>// text color of the  menus
</style>

在你的布局中像这样使用它。
<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/ToolBarStyle"
    app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
    android:background="@color/primary_pink"
    android:minHeight="?attr/actionBarSize"
    android:elevation="4dp"
    />

0

尝试更改为支持小部件。这对我有用。

app:actionViewClass="android.support.v7.widget.SearchView"

这是变更后的样子

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_search"
        app:showAsAction="always|withText"
        app:actionViewClass="android.support.v7.widget.SearchView"
        tools:context=".AppHome.HomeActivity"/>
</menu>

0
你可以像这样做:
首先,在你的Activity中删除默认标题:
getSupportActionBar().setDisplayShowTitleEnabled(false);

然后,您的工具栏布局可以是这样的:

<LinearLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/material_blue"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize" >

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:gravity="end"
            android:text="@string/app_name"
            android:textColor="@android:color/white" />
    </android.support.v7.widget.Toolbar>
</LinearLayout>

然后,在您的Activity中处理点击事件:

toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG,"Clicked");
    }
});

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