android 菜单项的标题未显示

xwbd5t1u  于 2023-04-18  发布在  Android
关注(0)|答案(3)|浏览(175)

菜单项的标题没有显示在片段中。我在菜单文件中有两个项目,第一个是带有图标和标签showAsAction=always的,用于在工具栏中显示图标。第二个只有标题。
我不知道这里出了什么问题。
菜单项的所有操作都在工作。例如下面。

menu_销售.xml

<?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:title="Scanner"
        android:id="@+id/miScanner"
        android:icon="@drawable/ic_baseline_qr_code_scanner"
        android:iconTint="@color/white"
        app:showAsAction="always"
        />

    <item
        android:title="Add Credit Record"
        android:id="@+id/miAddCreditRecord"
        />

</menu>

没有showAsAction='always'的项目在我的应用程序中不显示标题。
我的java代码膨胀菜单。

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_sale, menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.miScanner:
            scanCode();
            break;
        case R.id.miAddCreditRecord:
            showBottomSheetDialog();
            break;
    }
    return super.onOptionsItemSelected(item);
}

我的主题theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AzmiUnaniStore" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    
    <style name="NoActionBar" parent="Theme.AzmiUnaniStore">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="BottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
    </style>

    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        
    </style>
    
</resources>

可能性

标题显示得很完美,但当动作菜单颜色是白色,背景也是白色时,我看不到标题。
谢谢

wlp8pajw

wlp8pajw1#

问题是弹出菜单中的文本颜色。
MaterialToolbar中添加app:popupTheme属性。

<com.google.android.material.appbar.MaterialToolbar
        app:popupTheme="@style/AppTheme.PopupOverlay" 
        ../>

其中:

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary" >
    <!-- text color -->
    <item name="android:textColor">@color/...</item>  
</style>

b1uwtaje

b1uwtaje2#

我也面临着同样的问题。在我的情况下,我使用自定义工具栏的风格。

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarStyle" />

工具栏样式

<style name="ToolbarStyle" parent="AppTheme">
    <item name="android:textColorPrimary">@color/white</item>
</style>

因此,我将ToolbarStyle中的android:textColorPrimary更改为黑色,现在它工作正常

ccrfmcuu

ccrfmcuu3#

只需将此代码添加到themes.xml和themes.xml(night)文件中

<item name="android:textColor">@color/white</item>

相关问题