Android工具栏菜单项保持为空

3
我的Android应用程序有一个工具栏Toolbar,我想添加菜单项。工具栏被正确显示,但是展开菜单时菜单项为空。

android toolbar menu item is void

菜单结构在toolbar_menu.xml中定义:

toolbar_menu.xml

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/about"
        android:title="About"
        app:showAsAction="never"/>
</menu>

活动在布局文件activity_layout.xml中定义了工具栏。

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="Welcome"/>

    <!-- other widgets follow -->

</androidx.constraintlayout.widget.ConstraintLayout>

我设置工具栏的活动类是 MainActivity.java

MainActivity.java

// import dependencies

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_activity);

        final Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        this.setSupportActionBar(toolbar);
    }




    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        this.getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }
}

2
也许文本颜色是白色? - Shay Kin
你可以使用 app:showAsAction="always" 来检查文本是否为白色。 - Shay Kin
默认情况下,文本颜色是黑色,我猜。 - Ankit Mishra
这个应用程序使用的主题是什么? - Gabriele Mariotti
@GabrieleMariotti Theme.Deck.NoActionBar 是一种从 Theme.MaterialComponents.DayNight.DarkActionBar 继承的主题。我已经像在 https://dev59.com/acDqa4cB1Zd3GeqPXzHj#66977475 中发布的那样解决了这个问题。非常感谢。 - JCvanDamme
显示剩余3条评论
2个回答

2
问题是弹出菜单中的文字颜色。
在您的工具栏中添加app:popupTheme属性。
    <androidx.appcompat.widget.Toolbar
        app:popupTheme="@style/AppTheme.PopupOverlay" 
        ../>

使用:

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.DayNight.ActionBar" >
    <item name="android:textColor">@color/...</item>  
</style>

enter image description here


1
由于@GabrieleMariotti的评论,我意识到问题和解决方案与https://dev59.com/D3A75IYBdhLWcg3wAUJ9#63279563相同。
我正在使用自定义主题Theme.Deck.NoActionBar,它继承自Theme.MaterialComponents.DayNight.DarkActionBar

themes.xml

<style name="Theme.Deck" parent="Theme.MaterialComponents.DayNight.DarkActionBar">...</style>

<style name="Theme.Deck.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:itemTextAppearance">@style/menuItem</item> <!-- added to solve the problem -->
</style>

styles.xml

<!-- added to solve the problem -->
<style name="menuItem" parent="Widget.AppCompat.TextView.SpinnerItem">
    <item name="android:textColor">@color/black</item>
</style>

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