[英]Menu items are not displayed in the Android app
每個人! 我是初學者,正在開發移動應用程序。 朋友們,我的菜單項沒有顯示。 我點擊菜單上的三個點。 出現一個白色窗口,沒有菜單項的窗口。 程序代碼“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"
app:theme="@style/Theme.MAGIC">
<item
android:title="@string/search"
android:id="@+id/search_option"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView">
</item>
<item
android:id="@+id/setting_app"
android:title="@string/menu_settings"
android:icon="@drawable/ic_settings_app" />
<item
android:id="@+id/about_app"
android:title="@string/menu_about_application"
android:icon="@drawable/ic_info_about" />
</menu>
程序代碼“themes.xml”:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>
</resources>
程序代碼“MainActivity.java”
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem;
androidx.appcompat.widget.SearchView searchView;
getMenuInflater().inflate(R.menu.menu, menu);
menuItem = menu.findItem(R.id.search_option);
searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
如何顯示菜單項? 朋友,幫忙解決問題! 我正在移動設備上測試該應用程序。
當我創建我的菜單時,在onCreateOptionsMenu
覆蓋方法中我返回true
而不是super
。
此外,您是否覆蓋onOptionsItemSelected
方法來處理點擊? 您沒有發布該代碼。
這是 Google 關於創建菜單的文檔: https ://developer.android.com/guide/topics/ui/menus
問題出在 XML 文件中。 添加了以下行: <item name="android:textColor">@color/purple_200</item>
。 應用程序菜單如下所示: menu
完整程序代碼:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- The basic theme of the application -->
<style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- another part of the code -->
<!-- Text color, including menu text -->
<item name="android:textColor">@color/purple_200</item>
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.